很抱歉提出一个非常简单的问题,但是我需要解决它才能正确执行我的单独Python代码。
下面的代码是一个计算器,可以计算加减乘除,并将所有结果保存在过去的10个计算中,如果计算次数超过10次,则再次保存到第一个中。
但是当我运行这段代码时,出现错误NameError: name 'counter' is not defined
。
我希望有任何解决方案来解决此定义变量,因为即使定义了“计数器”,也会发生此错误。
def setup():
answer0 = 0
answer1 = 0
answer2 = 0
answer3 = 0
answer4 = 0
answer5 = 0
answer6 = 0
answer7 = 0
answer8 = 0
answer9 = 0
counter = 1
return answer0, answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, counter
def decideprocess():
global decision
decision = str(input("Enter + for adding, - for subtracting, * for multiply, / for deviding, and 'all' for see what all calculation results so far. :"))
return decision
def processes(decision, counter):
decision = decideprocess()
if decision == "+":
addition(counter)
decideprocess()
return counter
elif decision == "-":
subtraction(counter)
decideprocess()
return counter
elif decision == "*":
multiplecation(counter)
decideprocess()
return counter
elif decision == "/":
devision(counter)
decideprocess()
return counter
elif decision == "all":
print(answer0)
print(answer1)
print(answer2)
print(answer3)
print(answer4)
print(answer5)
print(answer6)
print(answer7)
print(answer8)
print(answer9)
decideprocess()
return answe1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10, counter, decision, result
def addition(counter):
a = float(input("Enter any number. :"))
b = float(input("Enter any number. :"))
result = a + b
print(result)
counter = counter + 1
assignment(answe1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10, counter, result)
return counter ,result
def subtraction(counter):
a = float(input("Enter any number. :"))
b = float(input("Enter any number. :"))
result = a - b
print(result)
counter = counter + 1
assignment(answe1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10, counter, result)
return counter ,result
def multiplecation(counter):
a = float(input("Enter any number. :"))
b = float(input("Enter any number. :"))
result = a * b
print(result)
counter = counter + 1
assignment(answe1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10, counter, result)
return counter ,result
def devision(counter):
a = float(input("Enter any number. :"))
b = float(input("Enter any number. :"))
result = a / b
print(result)
counter = counter + 1
assignment(answe1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10, counter, result)
return counter ,result
def assignment(answe1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10, counter, result):
while counter < 10:
assignmentanswer(counter, result)
if counter >= 10:
changecountervalue(counter)
assignmentanswerover10(result ,newcounter)
return answe1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10, counter, result, newcounter
def assignmentanswer(counter, result):
if counter == 1:
answer0 = result
if counter == 2:
answer1 = result
if counter == 3:
answer2 = result
if counter == 4:
answer3 = result
if counter == 5:
answer4 = result
if counter == 6:
answer5 = result
if counter == 7:
answer6 = result
if counter == 8:
answer7 = result
if counter == 9:
answer8 = result
if counter == 10:
answer9 = result
return counter, result
def changecountervalue(counter):
counter10 = int(counter/10)
newcounter = (counter10*10) - counter
return newcounter
def assignmentanswerover10(newcounter, result):
if newcounter == 1:
answer0 = result
if newcounter == 2:
answer1 = result
if newcounter == 3:
answer2 = result
if newcounter == 4:
answer3 = result
if newcounter == 5:
answer4 = result
if newcounter == 6:
answer5 = result
if newcounter == 7:
answer6 = result
if newcounter == 8:
answer7 = result
if newcounter == 9:
answer8 = result
if newcounter == 10:
answer9 = result
return newcounter, result
def showallresult(answe1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10):
print(answer0)
print(answer1)
print(answer2)
print(answer3)
print(answer4)
print(answer5)
print(answer6)
print(answer7)
print(answer8)
print(answer9)
def calculator():
setup()
decideprocess()
while decision == "+" or decision == "-" or decision == "*" or decision == "/" or decision == "all":
processes(decision, counter)
showallresult(answe1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10)
calculator()
答案 0 :(得分:0)
看起来有些代码需要缩进更多。
代替这个...
def processes(decision, counter):
decision = decideprocess()
if decision == "+":
addition(counter)
decideprocess()
return counter
elif decision == "-":
subtraction(counter)
decideprocess()
return counter
elif decision == "*":
multiplecation(counter)
decideprocess()
return counter
elif decision == "/":
devision(counter)
decideprocess()
return counter
elif decision == "all":
print(answer0)
print(answer1)
print(answer2)
print(answer3)
print(answer4)
print(answer5)
print(answer6)
print(answer7)
print(answer8)
print(answer9)
decideprocess()
return answe1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10, counter, decision, result
...尝试一下:
def processes(decision, counter):
decision = decideprocess()
if decision == "+":
addition(counter)
decideprocess()
return counter
elif decision == "-":
subtraction(counter)
decideprocess()
return counter
elif decision == "*":
multiplecation(counter)
decideprocess()
return counter
elif decision == "/":
devision(counter)
decideprocess()
return counter
elif decision == "all":
print(answer0)
print(answer1)
print(answer2)
print(answer3)
print(answer4)
print(answer5)
print(answer6)
print(answer7)
print(answer8)
print(answer9)
decideprocess()
return answe1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10, counter, decision, result