2017-03-28 43 views
0

該程序應該對給定的數字執行所描述的功能,但在給出第二個數字後出現錯誤。我怎樣才能讓一個Python 3.x程序允許用戶控制程序的功能?

代碼

num1=int(input("Give me a number: ")) 
num2=int(input("Give me another number: ")) 

add=int(("For addition, press [1].")) 
time.sleep(2) 
sub=int(print("For subtraction, press [2].")) 
time.sleep(2) 
div=int(("For division, press [3].")) 
time.sleep(2) 
multi=int(("For multiplication, press [4].")) 
time.sleep(2) 
print("Please input an option and press [RETURN].") 

if input == 1: 
    ans=num1+num2 
if input == 2: 
    ans=num1+num2 

如何,我可以在我的代碼來提高任何想法?

+0

'添加= INT(( 「對於另外,按[1]。」 ))'試圖將一個字符串轉換爲一個int,然後將其存儲在一個變量中。也許你的意思是'print'而不是'int'?否則,它會給你一個關於將字符串轉換爲int的錯誤。另外,如果輸入== 1沒有引用你創建的任何變量,所以我不能看到它會工作。 –

+0

@RandomDavis是的,我打算打印它,並讓[1] [2] [3] [4]選項激活代碼的加法/減法/乘法/除法方面,但我無法使其工作。 – WhiteTail

回答

0

如果你想獲得一個輸入這種替換:

​​
+0

這不是解決問題的辦法。 OP意在打印這些行,而不是從它們輸入。 –

+0

@RandomDavis我想*都* – WhiteTail

+0

@WhiteTail你只想得到一個輸入和打印4行,對吧?你所暗示的是,你想得到4個輸入,每個打印的行都有一個輸入。但是,你只是在最後檢查一個值,這意味着你只想得到一個輸入(除了前兩個數字)。此外,Abijith Mg的答案似乎是一個很好的解決方案。 –

1

更簡化的版本:

print "Basic calculator" 

num1=int(input("Enter number 1: ")) 
num2=int(input("Enter the other number: ")) 

choice=int(input("For addition, press [1].\n" 
    "For subtraction, press [2].\n" 
    "....\n" 
    "Please input the operation and press [RETURN].")) 
time.sleep(2) 

if choice == 1: 
    ans=num1+num2 
elif choice == 2: 
    ans=num1-num2 
    ... 

print "Answer is {}".format(ans) 
相關問題