我正在尝试为我的一个课程完成一个程序,但是我一直得到无效的语法,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
Run Code Online (Sandbox Code Playgroud)
我已更新以下代码以包括括号.
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()
Run Code Online (Sandbox Code Playgroud)
在此之前的那一行缺少')':
guess=int(input(num_x, "+", num_y, "=\n")
if guess==(num_x+num_y):
Run Code Online (Sandbox Code Playgroud)
正如Igor在评论中指出的那样,你还有其他一些也没有关闭括号的行,你也必须修复它们,否则你只需SyntaxError要再减少几行.你可能想要考虑使用一个有助于平衡括号和类似问题的编辑器 - 这肯定会让我的生活变得更轻松.
这在Python中并不像大多数其他语言那么常见,但它确实经常发生,以至于每当你得到一个莫名其妙的时候就应该习惯于查看前一行SyntaxError.
(在大多数语言中,几乎所有的表达式或语句可以继续到下一行.这不是真正在Python的,但如果有一个开放的括号,括号表达式被允许继续到下一行.)