※ 引述《doghib (lun)》之銘言:
: ※ 引述《purestone (天空之子)》之銘言:
: : 在網路上遍尋不到適合的範例,只好自己亂寫一個腦殘的小程式,但還是不喜歡,可以請
: : 哪位大大幫我改一下嗎??
: : #!/usr/bin/env python
: : #File name:calculation
: : x=int(raw_input('Enter an integer:'))
: : y=int(raw_input('Enter an integer:'))
: : A=x+y
: : B=x-y
: : C=x*y
: : D=x/y
: : print 'A is',A
: : print 'B is',B
: : print 'C is',C
: : print 'D is',D
: : 上述的小程式每次輸入數值後,都會出現四個答案,如果我只想要一個答案的時候,要怎
: : 作呢??拜託哪位大大指點一下..
: 用if 做條件判斷吧
: 上面加上
: oper = raw_input("Enter oper (+, -, *, /) : ")
: 輸出變成
: if oper == '+':
: print "x+y= ", x+y
: elif oper == '-':
: print "x-y= ", x-y
: 下面類推
: 如果剛開始學可以看一下 "周蟒" 的範例喔!!
--
門外漢 http://aockc.blogspot.com/
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.169.131.234
小弟寫了個雛型 供參考
# -*- coding: UTF-8 -*-
"""
檔名:asmd.py
系統:MS-Windows
描述:要求使用者輸入兩個數字,然後做四則運算。
"""
from os import system
from time import sleep
from sys import exit
def asmd(x,y,o):
if o=="+":
return x+y
elif o=="-":
return x-y
elif o=="*":
return x*y
elif o=="/":
return float(x)/float(y)
else:
print "無效輸入"
system("pause")
exit()
def main():
print "這個程式計算x、y兩數的四則運算:"
n=0
while not n:
try:
x=input("請輸入x:")
o=raw_input("請輸入+、-、*或/:")
y=input("請輸入y:")
n=1
except:
print "無效輸入"
i=3
while i>0:
print "倒數計時", i, "\r",
sleep(1)
i-=1
system("cls")
n=0
print x,o,y,"=",asmd(x,y,o)
if __name__=="__main__":
main()
print
system("pause")