我正在尝试为我的一个课程完成一个程序但是我的语法无效,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()
答案 0 :(得分:6)
之前的那一行缺少')':
guess=int(input(num_x, "+", num_y, "=\n")
if guess==(num_x+num_y):
正如Igor在评论中指出的那样,你还有其他一些也没有关闭括号的行,你也必须修复它们,否则你只需要另外几行SyntaxError
。你可能想要考虑使用一个有助于平衡括号和类似问题的编辑器 - 这肯定会让我的生活变得更轻松。
这在Python中并不像大多数其他语言那么常见,但它确实经常发生,当你得到一个无法解释的SyntaxError
时,你应该习惯于查看上一行。
(在大多数语言中,几乎任何表达式或语句都可以继续到下一行。在Python中不是这样 - 但如果有一个开括号,则括号表达式 允许继续下一行线。)