import random
import time
global score
global cpuscore
score = 1
cpuscore = 1
print("Current score CPU:Player = 1 , 1 ")
def plus():
rand1 = random.randint(2,99)
rand2 = random.randint(2,99)
ans = (rand1 + rand2)
print("What's", rand1, "+" ,rand2,)
quest = int(input())
if quest == ans:
print("Correct")
score + 1
else:
print("Incorrect")
cpuscore + 1
def multiply():
rand1 = random.randint(2,99)
rand2 = random.randint(2,99)
ans = (rand1 + rand2)
print("What's", rand1, "+" ,rand2,)
quest = int(input())
if quest == ans:
print("Correct")
score + 1
else:
print("Incorrect")
cpuscore + 1
def subtract():
rand1 = random.randint(2,99)
rand2 = random.randint(2,99)
ans = (rand1 + rand2)
print("What's", rand1, "+" ,rand2,)
quest = int(input())
if quest == ans:
print("Correct")
score + 1
else:
print("Incorrect")
cpuscore + 1
print("Welcome to eleven elves")
print("Please name your elves")
elf1 = input("Elf 1: ")
elf2 = input("Elf 2: ")
elf3= input("Elf 3: ")
elf4 = input("Elf 4: ")
elf5 = input("Elf 5: ")
elf6 = input("Elf 6: ")
elf7 = input("Elf 7: ")
elf8 = input("Elf 8: ")
elf9 = input("Elf 9: ")
elf10 = input("Elf 10: ")
elf11 = input("Elf 11: ")
print("So let me just clarrify, the name of your eleven elves throughout this quest will be")
clar = str.lower(input("Is this correct? (Yes or No) "))
if clar == ("yes"):
print("Ok, let's continue on")
else:
print("Would you to change the name of your elves?")
clar2 = str.lower(input("Yes or No "))
if clar2 == ("yes"):
elf1 = input("Elf 1: ")
elf2 = input("Elf 2: ")
elf3= input("Elf 3: ")
elf4 = input("Elf 4: ")
elf5 = input("Elf 5: ")
elf6 = input("Elf 6: ")
elf7 = input("Elf 7: ")
elf8 = input("Elf 8: ")
elf9 = input("Elf 9: ")
elf10 = input("Elf 10: ")
elf11 = input("Elf 11: ")
print("Elf names changed.")
else:
print("Ok no problem, let's continue on")
print(elf1, "enters a cave... The cave is full of monsters and creepy crawlys, a monster pops out of no where, the monster says.. Dear old dear old", elf1 ,"Why are you all by your self? You wanna get past, you gotta' get asked do you accept or run away?")
q1 = str.lower(input("Do you accept the challenge... ('you get the drift now yes or no....') "))
if q1 == ("yes"):
plus()
print("Current score CPU:Player", cpuscore, score)
else:
print(elf1, "runs away")
print(cpuscore)
我希望我的代码添加1到"得分"如果问题是正确的,那么它应该添加1到" cpuscore"但是当我运行代码并使问题正确时,它仍然将新分数打印为1,它真的令人困惑,我无法弄清楚原因。我是python的新手,所以我真的不会介意一些帮助。
问题仍然存在
Traceback (most recent call last):
File "N:\Open Me x\Computing\Mrs Farakh\Programming\Python\Assessment.py", line 139, in <module>
plus()
File "N:\Open Me x\Computing\Mrs Farakh\Programming\Python\Assessment.py", line 27, in plus
score += 1
UnboundLocalError: local variable 'score' referenced before assignment
答案 0 :(得分:0)
代码的问题是这个,
score + 1
应该是,
score = score + 1
您需要创建一个def函数来分配全局变量,否则全局函数完全没用。
希望这有帮助!
答案 1 :(得分:-1)
python中的整数是immutable
。对于 python 中的整数没有就地修改,例如 C 。您需要在每次增量后重新分配值。
替换:
score + 1
通过
score += 1 # or score = score + 1
和
cpuscore + 1 # or cpuscore = cpuscore + 1
通过
cpuscore += 1
在修改这些变量的每个函数中使变量全局化为:
def plus():
global score
global cpuscore
def subtract():
global score
global cpuscore