![](/img/trans.png)
[英]UnboundLocalError: local variable 'error' referenced before assignment
[英]UnboundLocalError: local variable referenced before assignment error
我嘗試了所有我找不到的解決方案
一個學校項目的調查,預計會給人的分數增加 100 分
你覺得你能幫忙嗎
代碼:
#QUESTION
q1 = "TRUE OR FALSE: The lightest atom is hydrogen"
q2 = "TRUE OR FALSE: Osmium is one of the densest atom if not the most"
q3 = "TRUE OR FALSE: chemistry is the branch of science that deals with the identification of the substances of which matter is composed; the investigation of their properties and the ways in which they interact, combine, and change; and the use of these processes to form new substances."
q4 = "TRUE OR FALSE: There are 200 elements discovered in history"
q5 = "TRUE OR FALSE: Dmitri Ivanovich Mendeleev was a Russian chemist and inventor. He is best known for formulating the Periodic Law and creating a version of the periodic table of elements."
#_____________________________________________________________________
q6 = "TRUE OR FALSE: The earth is the center of the earth"
q7 = "TRUE OR FALSE: The sun the the center of the earth"
q8 = "TRUE OR FALSE: The sun makes up most of the mass in our solar system"
q9 = "TRUE OR FALSE: Jupiter has the most moon in the solar system"
q10 = "TRUE OR FALSE: Mars's nick name is the red planet"
#____________________________________________________________________
q11 = "TRUE OR FALSE: Supernova's are the biggest explosions in the universe"
q12 = "TRUE OR FALSE: Gamma rays bursts are the biggest explosions the universe"
q13 = "TRUE OR FALSE: All stars turn into supernovas"
q14 = "TRUE OR FALSE: All stars turn into blacks holes"
q15 = "TRUE OR FALSE: All stars turn into neutron stars"
#____________________________________________________________________
q16 = "TRUE OR FALSE: Based on the model, Juptier is the biggest planet"
q17 = "TRUE OR FALSE: Based on the model, earth is the samllest planet"
q18 = "TRUE OR FALSE: Based on the model, neptune is the farthest planet to the sun"
q19 = "TRUE OR FALSE: Based on the model, the sun is the biggest object"
q20 = "TRUE OR FALSE: Based on the model the asteroid belt is located between Jupiter and Mars"
#____________________________________________________________________
#ANSWER KEY
a1 = "True"
a2 = "True"
a3 = "True"
a4 = "False"
a5 = "True"
a6 = "False"
a7 = "True"
a8 = "True"
a9 = "False"
a10 = "True"
a11 = "False"
a12 = "True"
a13 = "False"
a14 = "False"
a15 = "False"
a16 = "True"
a17 = "False"
a18 = "True"
a19 = "True"
a20 = "True"
global player1points
global player2points
player1points = int(0)
player2points = int(0)
startkey = "Start"
rulekey = "Rules"
def software(key):
if p1!=key and p2!=key:
print("Both of you got it wrong")
elif p2==key and p1!=key:
print(f"{p2name} got it right")
player2points = player2points+int(100)
elif p1==key and p2!=key:
print(f"{p1name} got it right")
player1points = player1points+int(100)
elif p1==key and p2==key:
print("both of you got it right")
player1points = player1points+int(100)
player2points = player2points+int(100)
start = input(f"Hello to the tester 9000\n\nType start to start\ntype rules to know how to play! ").capitalize().strip()
if start==rulekey:
rule = input("You need aleast 2 players to play this game\n\neverytime you get the question right you get 100 points\n\n the person with the most points after 20 questions is the winner\ntype start to start")
rule = "Start"
if start==startkey or rule==startkey:
p1name = input("what is your name player one: ")
p2name = input("what is your name player two: ")
print(f"time for you first question")
print(q1)
p1 = input(f"{p1name} turn").capitalize().strip()
p2 = input(f"{p2name} turn").capitalize().strip()
software(a1)
print(player1points)
print(player2points)
這是它給我的錯誤代碼:
Hello to the tester 9000
Type start to start
type rules to know how to play! rules
You need aleast 2 players to play this game
everytime you get the question right you get 100 points
the person with the most points after 20 questions is the winner
type start to startstart
what is your name player one: kunde
what is your name player two: chan
time for you first question
TRUE OR FALSE: The lightest atom is hydrogen
kunde turnfalse
chan turntrue
chan got it right
Traceback (most recent call last):
File "<string>", line 103, in <module>
File "<string>", line 71, in software
UnboundLocalError: local variable 'player2points' referenced before assignment
錯誤原因
改進
改進的代碼
#The error is your global statements should be inside function software since the global keyword allows us to modify variables outside of the current scope. You need it in
# The questions and answers should be in a data structure rather than each being its own variable
# This allows you to interate over the questions
# Most of your print statements you can use string literal rather than f-strings since they don't use a variable
# Unnecessarily using f-strings confuses the code reviewer since they are looking for a variable in your string
#QUESTION
questions = ["TRUE OR FALSE: The lightest atom is hydrogen",
"TRUE OR FALSE: Osmium is one of the densest atom if not the most",
"TRUE OR FALSE: chemistry is the branch of science that deals with the identification of the substances of which matter is composed; the investigation of their properties and the ways in which they interact, combine, and change; and the use of these processes to form new substances.",
"TRUE OR FALSE: There are 200 elements discovered in history",
"TRUE OR FALSE: Dmitri Ivanovich Mendeleev was a Russian chemist and inventor. He is best known for formulating the Periodic Law and creating a version of the periodic table of elements.",
"TRUE OR FALSE: The earth is the center of the earth",
"TRUE OR FALSE: The sun the the center of the earth",
"TRUE OR FALSE: The sun makes up most of the mass in our solar system",
"TRUE OR FALSE: Jupiter has the most moon in the solar system",
"TRUE OR FALSE: Mars's nick name is the red planet",
"TRUE OR FALSE: Supernova's are the biggest explosions in the universe",
"TRUE OR FALSE: Gamma rays bursts are the biggest explosions the universe"
"TRUE OR FALSE: All stars turn into supernovas",
"TRUE OR FALSE: All stars turn into blacks holes",
"TRUE OR FALSE: All stars turn into neutron stars",
"TRUE OR FALSE: Based on the model, Juptier is the biggest planet",
"TRUE OR FALSE: Based on the model, earth is the samllest planet",
"TRUE OR FALSE: Based on the model, neptune is the farthest planet to the sun",
"TRUE OR FALSE: Based on the model, the sun is the biggest object",
"TRUE OR FALSE: Based on the model the asteroid belt is located between Jupiter and Mars"]
#____________________________________________________________________
#ANSWER KEY
answers = ["True",
"True",
"True",
"False",
"True",
"False",
"True",
"True",
"False",
"True",
"False",
"True",
"False",
"False",
"False",
"True",
"False",
"True",
"True",
"True"]
def update_points(points, key):
'''
Updates points for player1 & player2
'''
print(p1, p2, key)
if p1!=key and p2!=key:
print("Both of you got it wrong")
elif p2==key and p1!=key:
print(f"{p2name} got it right")
# increment player 2 points
points[p2name] += 100
elif p1==key and p2!=key:
print(f"{p1name} got it right")
# increment player 1 points
points[p1name] += 100
elif p1==key and p2==key:
print("both of you got it right")
# increment both players points
points[p1name] += 100
points[p2name] += 100
return points # updated points
startkey = "Start"
rulekey = "Rules"
start = input('''Hello to the tester 9000
Type start to start
type rules to know how to play!''').capitalize().strip()
if start==rulekey:
rule = input("You need aleast 2 players to play this game\n\neverytime you get the question right you get 100 points\n\n the person with the most points after 20 questions is the winner\ntype start to start")
rule = "Start"
if start==startkey or rule==startkey:
p1name = input("what is your name player one: ")
p2name = input("what is your name player two: ")
# Initialize points for each player
points = {}
points[p1name] = 0
points[p2name] = 0
for i, q in enumerate(questions):
# loop over each question in list. Enumerate provides the index of the quesiton
print(f"time for question {i}")
print(questions[i])
# Get answers
p1 = input(f"{p1name} turn").capitalize().strip()
p2 = input(f"{p2name} turn").capitalize().strip()
# Update points
points = update_points(points, answers[i])
print(f"Player 1 points: {points[p1name]}")
print(f"Player 2 points: {points[p2name]}")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.