制作一个随机的一位数加法问题生成器,我最终得到了这个错误
import random
num = random.randint(0,9)
bothnumbers = []
score = 0
for i in range (10):
q = bothnumbers.append(int(input("What is the answer to: " ,num,"+",num )))
answer = sum(bothnumbers)
if q == answer:
score=+1
print("Your score is: ",score)
input
接受单个字符串,而不是
input("What is the answer to: " ,num,"+",num )
你可以做
input(f"What is the answer to: {num} + {num}")
bothnumbers.append
不返回任何结果,因此q
将是None
。我不知道你想用bothnumbers
做什么。你从来没有计算过num+num
,所以你没有办法检查答案。每次考试都会问同样的问题。你没有初始化score
我想你想要这个:
import random
score = 0
for _ in range(10):
num1 = random.randint(0,9)
num2 = random.randint(0,9)
q = int(input(f"What is the answer to {num1}+{num2}? " ))
if q == num1+num2:
score += 1
print( f"Your score was {score}")