Every time I run my program I get that message after answering the first question. I don't understand why this keeps happening because I have created the score variable before everything, I don't see why there is an issue. How do I solve this? This is my code:
score = 0
name = input("Before we start, what would you like me to call you? : ")
def greeting():
print ("Welcome to your Math review quiz,",name)
print("You will have to answer 5 questions")
def quiz1():
q1 = int (input("5 + 5 : "))
if q1 == 10:
print("Correct")
score = score + 1
else :
print ("Incorrect")
q2 = int (input ("5 + 10 : "))
if q2 == 15 :
print("Correct")
score = score + 1
else :
print("Incorrect")
q3 = int (input ("50 + 10 : "))
if q3 == 60 :
print("Correct")
score = score + 1
else :
print("Incorrect")
q4 = int (input ("50 + 50 : "))
if q4 == 100 :
print("Correct")
score = score + 1
else :
print("Incorrect")
greeting()
quiz1()
The scope of the variable score
is not in quiz1
. Therefore, I would pass it in, and return it as fit:
score = 0
name = input("Before we start, what would you like me to call you? : ")
def greeting():
print ("Welcome to your Math review quiz,",name)
print("You will have to answer 5 questions")
def quiz1(score):
q1 = int (input("5 + 5 : "))
if q1 == 10:
print("Correct")
score = score + 1
else :
print ("Incorrect")
q2 = int (input ("5 + 10 : "))
if q2 == 15 :
print("Correct")
score = score + 1
else :
print("Incorrect")
q3 = int (input ("50 + 10 : "))
if q3 == 60 :
print("Correct")
score = score + 1
else :
print("Incorrect")
q4 = int (input ("50 + 50 : "))
if q4 == 100 :
print("Correct")
score = score + 1
else :
print("Incorrect")
return score
greeting()
score = quiz1(score)
When you refer to a variable in a function definition (other than methods) python refers to the local veriable. You can edit score
as a global variable with the global
keyword.
score = 0
name = input("Before we start, what would you like me to call you? : ")
def greeting():
print ("Welcome to your Math review quiz,",name)
print("You will have to answer 5 questions")
def quiz1():
global score # add this line #
q1 = int (input("5 + 5 : "))
if q1 == 10:
print("Correct")
score = score + 1
else :
print ("Incorrect")
q2 = int (input ("5 + 10 : "))
if q2 == 15 :
print("Correct")
score = score + 1
else :
print("Incorrect")
q3 = int (input ("50 + 10 : "))
if q3 == 60 :
print("Correct")
score = score + 1
else :
print("Incorrect")
q4 = int (input ("50 + 50 : "))
if q4 == 100 :
print("Correct")
score = score + 1
else :
print("Incorrect")
greeting()
quiz1()
make score
global, add global score
in the first line in the function.
This might help
name = input("Before we start, what would you like me to call you? : ")
def greeting():
print ("Welcome to your Math review quiz,",name)
print("You will have to answer 5 questions")
def quiz1():
score = 0
q1 = int (input("5 + 5 : "))
if q1 == 10:
print("Correct")
score = score + 1
else :
print ("Incorrect")
q2 = int (input ("5 + 10 : "))
if q2 == 15 :
print("Correct")
score = score + 1
else :
print("Incorrect")
q3 = int (input ("50 + 10 : "))
if q3 == 60 :
print("Correct")
score = score + 1
else :
print("Incorrect")
q4 = int (input ("50 + 50 : "))
if q4 == 100 :
print("Correct")
score = score + 1
else :
print("Incorrect")
greeting()
quiz1()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.