Python 3.x Python';遗忘';变量?
因此,我试图将变量写入一个单独的.txt文件,python说我的变量没有定义,尽管它在程序中使用了相同的变量 在这里,我分配并使用变量运算符_选项:Python 3.x Python';遗忘';变量?,python-3.x,Python 3.x,因此,我试图将变量写入一个单独的.txt文件,python说我的变量没有定义,尽管它在程序中使用了相同的变量 在这里,我分配并使用变量运算符_选项: operator_choice = input ("Ok Mate, now chose your function: multiplication, division, addition or subtraction!") if operator_choice in times: while Que
operator_choice = input ("Ok Mate, now chose your function: multiplication, division, addition or subtraction!")
if operator_choice in times:
while Question < 12:
Mathmultiplication();
break
if operator_choice in division:
while Question < 12:
Mathdivision();
break
if operator_choice in subtraction:
while Question < 12:
Mathsub();
break
if operator_choice in addition:
while Question < 12:
Mathadd();
break
elif operator_choice not in total_difficulty:
print ("\nNot an option, try again")
它给了我这个错误:
NameError: name 'operator_choice' is not defined
有人能告诉我发生了什么,为什么以及如何解决吗?
这不是在函数中,而是在while循环中
完整代码如下所示:
import random
#just some variables
times = {'*'}
divide = {'/'}
other = {'-','+'}
myName = input ("So user, whats your name?... ")
#Operator stuff
correct = 0
incorrect = 0
Question = 0
#Defining The Math
def math():
global Question
global correct
global incorrect
Question = Question + 1
print ("________________________________________________________________________________")
print ("Ok Then, This is question:", Question)
print (str(number) + operator_choice + str(second_number) , "= ?")
answer = eval(str(number) + operator_choice + str(second_number))
guess = input ()
if guess == str (answer):
print('Correct!')
correct = correct + 1
elif guess != str(answer):
print("Sorry, but that's wrong!The answer was:", answer, "Onto the next question then!")
incorrect = incorrect +1
def division():
global Question
global correct
global incorrect
Question = Question + 1
print ("________________________________________________________________________________")
print ("Ok Then, This is question:", Question,)
number = (random.randint (12,24) * level)
second_number = (random.randint(2,12)* level)
print (number,"/",second_number)
answer = number//second_number
guess = input ()
if guess == str (answer):
print('Correct!')
correct = correct + 1
elif guess != str(answer):
print("Sorry, but that's wrong!The answer was:", answer, "Onto the next question then!")
incorrect = incorrect +1
#Difficulty
four = {'Impossible','impossible','4'}
total_difficulty = {'1','2','3','4'}
while True:
print ("Ok,; Choose a difficulty: 1 , 2, 3 or 4(Changing operators, Near impossible!)...")
difficulty_choice = input ()
if difficulty_choice not in total_difficulty:
print ("Not an option, try again")
else: break
level = int(difficulty_choice)
if difficulty_choice not in four:
print ("________________________________________________________________________________")
while True:
operator_choice = input ("Ok Mate, now chose your function: \nmultiplication (input * )\ndivision (input / ) \naddition (input + ) \nsubtraction (input - )\n...")
if operator_choice in times:
while Question < 12:
number = (random.randint (2,6) * level)
second_number = (random.randint (2,6) * level)
math();
break
if operator_choice in divide:
while Question < 12:
division();
break
if operator_choice in other:
while Question < 12:
number = (random.randint (1,25) * level)
second_number = (random.randint (1,25) * level)
math();
break
elif operator_choice not in total_difficulty:
print ("\nNot an option, try again")
#Randomised operators
elif difficulty_choice in four:
print ("you have chosen: Random. Be warned, These questions are HARD, and the operator WILL change!")
Question = 0
while Question < 12:
Question = Question + 1
print ("________________________________________________________________________________")
print ("Ok Then, This is question:", Question)
ops = ['*', '/', '+', '-']
op = random.choice (ops)
number = random.randint (2,25)
number2 = random.randint (2,25)
number3 = eval(str(number) + op + str(number2))
number3 = int(number3)
answer = eval(str(number3) + op + str(number))
answer = int (answer)
print (number3, op, number)
guess = input()
if guess == str (answer):
print('Correct!')
correct = correct + 1
elif guess != str(answer):
print("Sorry, but that's wrong!The answer was:", answer, "Onto the next question then!")
incorrect = incorrect +1
#Outros
print ("________________________________________________________________________________")
print ("Game Over!", myName," got:", correct,"questions right, and",incorrect,"Wrong!")
if correct == 12:
print ("Congratz",myName,"! You got a perfect score! See ya around",myName,"!")
else: print ("good luck next time! Bye!")
#save results
correct = str(correct)
incorrect = str(incorrect)
Results = open (r"Results.txt", 'a')
Results.write ("\nHello There Teacher! " + myName + " got: " + correct + "correct, and: " + incorrect + " wrong on level: " + difficulty_choice + ", ")
Results.write (operator_choice)
Results.close()
随机导入
#只是一些变量
时间={'*}
除法={'/'}
其他={'-','+'}
myName=输入(“用户,你叫什么名字?……”)
#接线员
正确=0
不正确=0
问题=0
#定义数学
def math():
全球问题
全局正确
全局错误
问题=问题+1
打印(“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
打印(“好吧,这是问题:”,问题)
打印(str(编号)+运算符选择+str(第二个编号),“=?”)
答案=评估(str(编号)+操作员选择+str(第二个编号))
猜测=输入()
如果猜测=str(回答):
打印('正确!')
正确=正确+1
我猜str(答复):
打印(“对不起,那是错的!答案是:”,回答,“然后转到下一个问题!”)
不正确=不正确+1
def division():
全球问题
全局正确
全局错误
问题=问题+1
打印(“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
打印(“好吧,这是问题:,问题,)
数字=(random.randint(12,24)*级别)
第二个_数=(random.randint(2,12)*级)
打印(编号“/”,第二个编号)
答案=数字//第二个数字
猜测=输入()
如果猜测=str(回答):
打印('正确!')
正确=正确+1
我猜str(答复):
打印(“对不起,那是错的!答案是:”,回答,“然后转到下一个问题!”)
不正确=不正确+1
#困难
四={‘不可能’、‘不可能’、‘4’}
总难度={'1','2','3','4'}
尽管如此:
打印(“好的,选择一个难度:1、2、3或4(改变操作符,几乎不可能!)”)
难度\选择=输入()
如果难度选择不是总难度:
打印(“不是选项,请重试”)
其他:休息
级别=整数(难度\选择)
如果您选择的不是四项:
打印(“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
尽管如此:
运算符\u choice=input(“好的,伙计,现在选择您的函数:\n乘法(input*)\n乘法(input/)\n条件(input+)\n乘法(input-)\n…”)
如果操作员选择时间:
当问题<12时:
数字=(random.randint(2,6)*级别)
第二个_数=(random.randint(2,6)*级)
数学();
打破
如果运算符_选择除法:
当问题<12时:
分部();
打破
如果操作员选择其他:
当问题<12时:
数字=(random.randint(1,25)*级别)
第二个_数=(random.randint(1,25)*级)
数学();
打破
elif操作员选择不完全困难:
打印(“\n不可选,请重试”)
#随机算子
elif难度的四种选择:
打印(“您已选择:随机。请注意,这些问题很难回答,操作员将更改!”)
问题=0
当问题<12时:
问题=问题+1
打印(“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
打印(“好吧,这是问题:”,问题)
ops=['*'、'/'、'+'、'-']
op=随机选择(ops)
number=random.randint(2,25)
number2=random.randint(2,25)
编号3=评估(str(编号)+op+str(编号2))
number3=int(number3)
答案=eval(str(编号3)+op+str(编号))
答案=int(答案)
打印(编号3,op,编号)
猜测=输入()
如果猜测=str(回答):
打印('正确!')
正确=正确+1
我猜str(答复):
打印(“对不起,那是错的!答案是:”,回答,“然后转到下一个问题!”)
不正确=不正确+1
#奥特罗斯
打印(“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
打印(“游戏结束!”,我的名字,“得到:”,正确,“问题正确”,不正确,“错误!”)
如果正确==12:
打印(“恭喜”,我的名字“!你得了满分!回头见”,我的名字“!”)
其他:打印(“祝你下次好运!再见!”)
#保存结果
correct=str(正确)
不正确=str(不正确)
结果=打开(r“Results.txt”,“a”)
结果。写下(“\nHello There Teacher!”+myName+”得到:“+correct+”正确,以及:“+correct+”错误,级别:“+combiness\u choice+”,”)
Results.write(操作员选择)
结果关闭()
在发布完整的代码之前,您无法获得完整的答案。但是,我可以为您提供一个可能的解决方案:
您在上面发布的代码位于单独的函数中,因此,operator\u choice
的范围仅在该函数中定义。确保在函数中返回变量,或调用结果。在该函数中写入
例如:
>>> def foo():
... bar = 1
...
>>> bar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'bar' is not defined
>>> foo()
>>> bar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'bar' is not defined
>>def foo():
... 巴=1
...
>>>酒吧
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
NameError:未定义名称“bar”
>>>foo()
>>>酒吧
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
NameError:未定义名称“bar”
是结果。使用与上述代码相同的功能编写
?是的,请发布一个完整的工作可复制示例:,因此完整代码已显示。有人能帮忙吗?
>>> def foo():
... bar = 1
...
>>> bar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'bar' is not defined
>>> foo()
>>> bar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'bar' is not defined