我正在尝试在python中进行测验,但我一直收到无效的语法错误。
#This is for addition questions.
if (question=add) <---- That is where i get the error
for i in range(0,10):
first_number_add=random.randint(1,50)
second_number_add=random.randint(1,50)
answer=int(input(str(first_number_add) + '+' + str(second_number_add) + '='))
sum=first_number_add+second_number_add
if (answer==sum):
print ('Correct')
stats['correct'] += 1
else:
print ('Incorrect.')
stats['incorrect'] += 1
答案 0 :(得分:4)
要进行比较,您需要使用==
:
if question==add: #you dont need bracket here
# do your stuff
sum
是内置的sum函数,不要将它用于变量名
答案 1 :(得分:1)
替换为if question == add:
并缩进代码的其余部分。
另外,请勿使用sum
作为变量名称。这是一个很有用的功能。
if question == add:
for i in range(10):
first_number_add = random.randint(1,50)
second_number_add = random.randint(1,50)
answer = int(input(str(first_number_add) + '+' + str(second_number_add) + '='))
the_sum = first_number_add + second_number_add
if answer == the_sum:
print('Correct')
stats['correct'] += 1
else:
print('Incorrect.')
stats['incorrect'] += 1
答案 2 :(得分:0)
if question==add:
for i in range(0,10):
first_number_add=random.randint(1,50)
second_number_add=random.randint(1,50)
answer=int(input(str(first_number_add) + '+' + str(second_number_add) + '='))
sum=first_number_add+second_number_add
if answer==sum:
print ('Correct')
stats['correct'] += 1
else:
print ('Incorrect.')
stats['incorrect'] += 1
你不需要parantheses。在for循环和if语句之后,你还必须在缩进中放置4个空格。作为@Joel评论,4个空格是标准的,你可以将它改为8-16等。
答案 3 :(得分:0)
if question==add:
for i in range(0,10):
first_number_add=random.randint(1,50)
second_number_add=random.randint(1,50)
answer=int(input(str(first_number_add) + '+' + str(second_number_add) + '='))
sum=first_number_add+second_number_add
if answer==sum:
print ('Correct')
stats['correct'] += 1
else:
print ('Incorrect.')
stats['incorrect'] += 1
在python中不需要括号...请记住,为每个块和循环语句提供一致的缩进(相同数量的空格)。