I am new to python and im wondering if anyone can help me with my issue.
def run_game():
'''
This is where the game takes place and how it runs.
'''
game_count = 0
score = 0
questions = ["What is the first pokemon in the pokedex?","How many generations of Pokemon are there currently?","Who is the water starter of the sinnoh region?","Which evil team was featured in both generation one and two?","What does eevee evolve into when leveled up near a glacial rock?","Which Pokemon is the the third of the creation trio?","Which ghost type Pokemon holds a mask of their face when they were human?","What item was added in Generation 7 that allows certain Ice Type Pokemon to evolve?","Which Pokemon is the god of the Pokemon universe?","According to the Pokedex: Which Pokemon can leap over mountain by using Splash, after it has lived for many years?"]
answers = ["Bulbasaur","Eight","Piplup","Team Rocket","Glaceon,","Giratina","Yanmask","Ice Stone","Arceus","Magikarp"]
while game_count < 1:
r = randint(0, len(questions)-1)
print(questions[r])
qanswer = input("Answer:")
if qanswer == answers[r]:
score = score 1
else:
print("Wrong answer!")
game_count = game_count 1
return score
So when this is run it goes through and when it returns the score it is an integer.
But once it hits main the value score comes up as none.
def main():
score = start_game()
game_end(score)
play_again()
Does anyone know how to fix this?
from random import randint
def start_game():
'''
This will ask user if they want to play or not, it then calls upon run_game().
'''
start = input("Would you like to take the pokemon quiz?(Y/N):")
if start.upper() == "Y":
run_game()
else:
quit()
def run_game(): ''' This is where the game takes place and how it runs. '''
game_count = 0
score = 0
questions = ["What is the first pokemon in the pokedex?","How many generations of Pokemon are there currently?","Who is the water starter of the sinnoh region?","Which evil team was featured in both generation one and two?","What does eevee evolve into when leveled up near a glacial rock?","Which Pokemon is the the third of the creation trio?","Which ghost type Pokemon holds a mask of their face when they were human?","What item was added in Generation 7 that allows certain Ice Type Pokemon to evolve?","Which Pokemon is the god of the Pokemon universe?","According to the Pokedex: Which Pokemon can leap over mountain by using Splash, after it has lived for many years?","If the fire on this Pokemon's tail goes out, it will die. Which Pokemon is this?","What is the Legendary Pokemon of the Sun?","What is the Legendary Pokemon of the Moon?","Which is more effective against Water Type Pokemon? Ice,Electricity, or Steel?"]
answers = ["Bulbasaur","Eight","Piplup","Team Rocket","Glaceon,","Giratina","Yanmask","Ice Stone","Arceus","Magikarp","Charmander","Solgaleo","Lunala","Electricity"]
while game_count < 1:
r = randint(0, len(questions)-1)
print(questions[r])
qanswer = input("Answer:")
if qanswer == answers[r]:
score = score 1
else:
print("Wrong answer!")
game_count = game_count 1
return score
def game_end(score): print("You scored", score, "points out of 5!")
def play_again():
pa = input("Would you like to play again?(Y/N):")
while pa.upper() == "Y":
score = run_game()
game_end(score)
play_again()
print("\n","Thank you for playing!")
quit()
def main(): score = start_game() game_end(score) play_again()
main()
Edit: I have added all the code here
CodePudding user response:
I think that score must be a global variable declared in main. Show the whole script, but I guess it is the trouble.
CodePudding user response:
Fast answer is: put a return
in start_game()
like this:
def start_game():
'''
This will ask user if they want to play or not, it then calls upon run_game().
'''
start = input("Would you like to take the pokemon quiz?(Y/N):")
if start.upper() == "Y":
return run_game()
else:
quit()
If I find more time, i wil extend my answer.
Edit
This is what I'd make it, attempting to respect your original work and purposes:
import random
class Game:
def __init__(self):
self.questions = [
"What is the first pokemon in the pokedex?",
"How many generations of Pokemon are there currently?",
"Who is the water starter of the sinnoh region?",
"Which evil team was featured in both generation one and two?",
"What does eevee evolve into when leveled up near a glacial rock?",
"Which Pokemon is the the third of the creation trio?",
"Which ghost type Pokemon holds a mask of their face when they were human?",
"What item was added in Generation 7 that allows certain Ice Type Pokemon to evolve?",
"Which Pokemon is the god of the Pokemon universe?",
"According to the Pokedex: Which Pokemon can leap over mountain by using Splash, after it has lived for "
"many years?",
"If the fire on this Pokemon's tail goes out, it will die. Which Pokemon is this?",
"What is the Legendary Pokemon of the Sun?",
"What is the Legendary Pokemon of the Moon?",
"Which is more effective against Water Type Pokemon? Ice, Electricity, or Steel?"
]
self.answers = [
"Bulbasaur", "Eight", "Piplup", "Team Rocket", "Glaceon,",
"Giratina", "Yanmask", "Ice Stone", "Arceus",
"Magikarp", "Charmander", "Solgaleo", "Lunala", "Electricity"
]
def run(self):
play_again = True
while play_again:
score = 0
game_count = 0
while game_count < 5:
index = random.randint(0, len(self.questions)-1)
question = self.questions[index]
answer = self.answers[index]
print(question)
ui_answer = input("Answer: ")
if ui_answer == answer:
score = 1
else:
print("Wrong answer!")
game_count = 1
print(f"You scored {score} points out of 5!")
answer = input("Would you like to play again?(Y/N): ")
play_again = answer.upper() == "Y"
print("\nThank you for playing!")
if __name__ == '__main__':
game = Game()
game.run()
Note: this is written in Python 3. In Python 2 you must change line print(f"You scored {score} points out of 5!")
as print("You scored {s} points out of 5!").format(s=score)
Simplest version
But maybe you'll prefer this simplest version:
import random
questions = [
"What is the first pokemon in the pokedex?",
"How many generations of Pokemon are there currently?",
"Who is the water starter of the sinnoh region?",
"Which evil team was featured in both generation one and two?",
"What does eevee evolve into when leveled up near a glacial rock?",
"Which Pokemon is the the third of the creation trio?",
"Which ghost type Pokemon holds a mask of their face when they were human?",
"What item was added in Generation 7 that allows certain Ice Type Pokemon to evolve?",
"Which Pokemon is the god of the Pokemon universe?",
"According to the Pokedex: Which Pokemon can leap over mountain by using Splash, after it has lived for many years?",
"If the fire on this Pokemon's tail goes out, it will die. Which Pokemon is this?",
"What is the Legendary Pokemon of the Sun?",
"What is the Legendary Pokemon of the Moon?",
"Which is more effective against Water Type Pokemon? Ice, Electricity, or Steel?"
]
answers = [
"Bulbasaur", "Eight", "Piplup", "Team Rocket", "Glaceon,", "Giratina", "Yanmask", "Ice Stone", "Arceus",
"Magikarp", "Charmander", "Solgaleo", "Lunala", "Electricity"
]
def start_game():
play_again = True
while play_again:
run_game()
answer = input("Would you like to play again?(Y/N): ")
play_again = answer.upper() == "Y"
print("\nThank you for playing!")
def run_game():
score = 0
game_count = 0
while game_count < 5:
index = random.randint(0, len(questions) - 1)
question = questions[index]
answer = answers[index]
print(question)
ui_answer = input("Answer: ")
if ui_answer == answer:
score = 1
else:
print("Wrong answer!")
game_count = 1
print(f"You scored {score} points out of 5!")
if __name__ == '__main__':
start_game()