Python 随机乘法问题
我正在为学校写一段代码,用来问随机乘法问题,计算问题的数量,以及正确的问题数量。当我运行此代码时,会出现一条错误消息,上面说: TypeError:Python 随机乘法问题,python,function,Python,Function,我正在为学校写一段代码,用来问随机乘法问题,计算问题的数量,以及正确的问题数量。当我运行此代码时,会出现一条错误消息,上面说: TypeError:question()接受0个位置参数,但给出了1个 我能做些什么来解决这个问题。 以下是我目前的代码: import random noqc=0 #noqc = number of questions correct noqtrack=1 #noqtrack = "number of questions tr
question()
接受0个位置参数,但给出了1个
我能做些什么来解决这个问题。
以下是我目前的代码:
import random
noqc=0 #noqc = number of questions correct
noqtrack=1 #noqtrack = "number of questions tracker"
def question():
x=random.randint(0,12)
z=random.randint(0,12)
y=("What is", x, "times", z, "?")
a=int(input(y))
if a==x*z:
print("Correct")
noqc=noqc+1
else:
print("Wrong, the answer was:",x*z,)
noq=int(input("How many questions would you like to do?")) #noq = number of questions
for i in range(0, noq):
print(noqtrack)
noqtrack=noqtrack+1
question(noqc) #<------
随机导入
noqc=0#noqc=正确的问题数量
noqtrack=1#noqtrack=“问题数量跟踪器”
定义问题():
x=random.randint(0,12)
z=random.randint(0,12)
y=(“是什么”,x,“乘以”,z,”)
a=int(输入(y))
如果a==x*z:
打印(“正确”)
noqc=noqc+1
其他:
打印(“错,答案是:”,x*z,)
noq=int(输入(“你想问多少问题?”)#noq=问题数量
对于范围内的i(0,noq):
打印(noqtrack)
noqtrack=noqtrack+1
问题(noqc)#出现错误是因为函数question()
的定义不接受任何参数(确切地说是位置参数),但您使用参数noqc
调用question(noqc)
纠正代码的一种方法可能是从question
函数返回值。如果答案正确,则返回1
,否则返回0
试试这个:
import random
noqc=0 #noqc = number of questions correct
noqtrack=1 #noqtrack = "number of questions tracker"
def question():
x = random.randint(0, 12)
z = random.randint(0, 12)
y = ("What is", x, "times", z, "?")
a = int(input(y))
if a == x*z:
print("Correct")
return 1
else:
print("Wrong, the answer was:", x*z,)
return 0
# noq = number of questions
noq = int(input("How many questions would you like to do?"))
for i in range(noq):
print(noqtrack)
noqtrack = noqtrack+1
noqc += question()
除了Shubham所说的之外,我认为您缺乏对Python作用域如何工作的理解。这里有一些代码示例,演示了局部作用域和全局作用域是如何工作的
x = 0
def wont_modify_1(x):
# this will make a copy of x in the local scope
# then increment it by one
x += 1
# prints 1
print ("x inside wont_modify_1 = {}".format(x))
wont_modify_1(x)
# prints 0, the variable outside the function wasn't modified
print ("x outside wont_modify_1 = {}".format(x))
y = 0
def won_t_modify_2():
# variable y does not exist in this scope
# it will be created here and assigned 1 to it
y = 1
# prints the value of the local variable, 1
print ("y inside wont_modify_2 = {}".format(y))
won_t_modify_2()
# the global variable is a different object
# it wasn't modified, it prints 0
print ("y outside wont_modify_2 = {}".format(y))
z = 0
def won_t_modify_3():
# z does not exists in this scope, so it can't be incremented by one
# this will raise an error
# UnboundLocalError: local variable 'z' referenced before assignment
z += 1
print ("z inside wont_modify_3 = {}".format(z))
won_t_modify_3()
print ("z outside wont_modify_3 = {}".format(z))
好的,你真的需要使用更多的描述性变量名
不要使用noqc
尝试使用numofforrectawners
或者像correct
这样简单的工具,这样会更具可读性,几乎肯定会让你在课堂上获得更多分数
此外,我将以不同的方式处理此循环:
noq = int(input("How many questions would you like to do?"))
for i in range(noq):
print(noqtrack)
noqtrack = noqtrack+1
noqc += question()
尝试将其更改为:
numOfCorrectQuestions = int(input("How many questions would you like to do?"))
while numOfCorrectAwnsers < numOfCorrectQuestions:
question()
numOfTotalQuestions += 1
numOfCorrectQuestions=int(输入(“您想问多少问题?”)
而NumofCorrectOwners
def-question(noqc)
而不是def-question()
。或者,调用不带参数的函数(即只调用question()
)并在函数定义中写入全局noqc
。