[英]I just cant figure out why my code throws a syntax error when I add int() to a input
首先是我的代碼:
def main():
def add(x,y):
return x + y
def subtract(x,y):
return x - y
def multiple(x,y):
return x * y
def divide(x,y):
return x / y
user_choice = int(input("Choose calculation: 1. add 2. subtract 3. divide 4. multiple \n")
dog = int(input("Enter first number: "))
cat = int(input("Enter second number: "))
if user_choice == '1':
print(dog,"+",cat,"=", add(dog,cat))
elif user_choice == '2':
print(dog,"-",cat,"=", subtract(dog,cat))
elif user_choice == '3':
print(dog,"*",cat,"=", multiply(dog,cat))
elif user_choice == '4':
print(dog,"/",cat,"=", divide(dog,cat))
else:
print("Invalid input")
main()
while True:
restart = input("Would you like to restart? (y/n)")
if restart == 'y':
main()
elif restart == 'n':
print('have a nice day!')
break
else:
print("invalid input. Please enter y or n.")
所以我建立了一個非常簡單的計算器,可以進行加,乘,減和除法運算。 現在,問題是當我運行此確切代碼時,出現語法錯誤,調出第20行
dog = int(input("Enter first number: "))
^
SyntaxError: invalid syntax
我無法弄清楚為什么這行是錯誤的,因為我查看了我構建的另一個小程序,它基本上具有相同的行。 經過一番擺弄之后,我設法通過從user_choice輸入中刪除int()來修復了該程序。 我只想知道為什么正是這個解決了這個問題? 並且生產線不起作用有其他原因嗎? 感謝您的輸入!
您在此行的末尾缺少括號(固定在下面):
user_choice = int(input("Choose calculation: 1. add 2. subtract 3. divide 4. multiple \n"))
此外, multiply
功能拼寫錯誤。
剛剛注意到了另一個問題, if user_choice == 1
,則if user_choice
檢查應該檢查整數而不是字符串if user_choice == 1
if user_choice == '1'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.