我想完成我的一個類的程序,但我一直得到無效的語法,python對我來說是非常新的,所以忍受我的無知。python無效的語法*更新
無效的語法彈出在「if guess ==(num_x + num_y)」冒號:「 如果有人可以幫助,我會非常感激。
更新* 固定括號謝謝abarnert但現在我有以下SyntaxError
Traceback (most recent call last):
File "C:\Users\Franz\Desktop\randomstudy.py", line 48, in <module>
main()
File "C:\Users\Franz\Desktop\randomstudy.py", line 25, in main
guess=int(input(num_x, "-", num_y, "=\n"))
TypeError: input expected at most 1 arguments, got 4
我已經更新了下面的代碼,包括括號。
def main():
import random
num_x=random.randint(-50,50)
num_y=random.randint(-50,50)
op=random.randint(0,3)
control=True
print("Welcome! Here is your random problem:\n")
if op==0:
while True:
guess=int(input(num_x, "+", num_y, "=\n"))
if guess==(num_x+num_y):
print("Congratulations! You have answered the problem correctly!")
break
else:
print("I’m sorry, that is not correct. Please try again.")
elif op==1:
while True:
guess=int(input(num_x, "-", num_y, "=\n"))
if guess==(num_x-num_y):
print("Congratulations! You have answered the problem correctly!")
break
else:
print("I’m sorry, that is not correct. Please try again.")
elif op==2:
while True:
guess=int(input(num_x, "*", num_y, "=\n"))
if guess==(num_x*num_y):
print("Congratulations! You have answered the problem correctly!")
break
else:
print("I’m sorry, that is not correct. Please try again.")
elif op==3:
while True:
guess=int(input(num_x, "/", num_y, "=(Please round to two decimal places)\n"))
if guess==(num_x/num_y):
print("Congratulations! You have answered the problem correctly!")
break
else:
print("I’m sorry, that is not correct. Please try again.")
main()
將缺少的')'添加到int的末尾(輸入(num_x,「+」,num_y,「= \ n」)'(爲其他兩個'int(輸入(..'))加上相同的值 –
將來,不要只說「無效的語法」,粘貼實際的'SyntaxError'和回溯。這很容易,但是通常需要更多的工作才能找到問題,並且如果我們可以看到所有的詳細信息Python給了你 – abarnert