我已经研究过这个主题,找不到相关的答案,这里是我的代码:
#Imports#
import random
from operator import add, sub, mul
import time
from random import choice
#Random Numbers#
beg1 = random.randint(1, 10)
beg2 = random.randint(1, 10)
#Variables + Welcoming message#
correct = 0
questions = 10
print ("Welcome to the Primary School Maths quiz!!")
print ("All you have to do is answer the questions as they come up!")
time.sleep(1)
#Name#
print("Enter your first name")
Fname = input("")
print ("Is this your name?" ,Fname)
awnser = input("")
if awnser == ("yes"):
print ("Good let's begin!")
questions()
if input == ("no"):
print("Enter your first name")
Fname = input("")
print ("Good let's begin!")
#Question Code#
def questions():
for i in range(questions):
ChoiceOp = random.randint (0,2)
if ChoiceOp == "0":
print (("What is " +beg1 ,op ,beg2))
begAns = input("")
if int(begAns) == beg1*beg2:
print("That's right -- well done.\n")
correct = correct +1
else:
print("No, I'm afraid the answer is ",begAns)
if ChoiceOp == "1":
print (("What is " +beg1 ,op ,beg2))
begAns = input("")
if int(begAns) == beg1-beg2:
print("That's right -- well done.\n")
correct = correct +1
else:
print("No, I'm afraid the answer is ",begAns)
if ChoiceOp == "2":
print (("What is " +beg1 ,op ,beg2))
begAns = input("")
if int(begAns) == beg1+beg2:
print("That's right -- well done.\n")
correct = correct +1
else:
print("No, I'm afraid the answer is ",begAns)
questions()
如果我非常诚实地说我不太确定是什么问题,那么这个精彩网站帮助我的代码遇到了很多问题,但无论如何这段代码都设计为向小学生提出10个随机的加法,减法和乘法问题任何帮助我都要提前感谢! :D
答案 0 :(得分:0)
您有一个函数def questions()
和一个变量questions = 10
。这在Python中不起作用;每个名称只能引用一件事:一个变量,一个函数,一个类,但不是每个名称中的一个,因为它是可能的,例如,在Java。
要解决此问题,请将您的变量重命名为例如num_questions = 10
或您的函数,例如def ask_question()
另请注意,在实际定义之前调用questions
函数。同样,这适用于其他一些语言,但不适用于Python。将您的def quesitons
置于顶部,输入提示位于下方或其他功能中,例如: def main()
。