[英]After going through the function a value I am returning becomes none
I am new to python and im wondering if anyone can help me with my issue.我是 python 的新手,我想知道是否有人可以帮助我解决我的问题。
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.因此,当它运行时,它会通过,当它返回分数时,它是 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从随机导入 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. def run_game(): ''' 这是游戏发生的地方和运行方式。 ''' '''
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 game_end(score): print("你得分了", score, "满分5分!")
def play_again(): 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() def main(): score = start_game() game_end(score) play_again()
main()主要的()
Edit: I have added all the code here编辑:我在这里添加了所有代码
I think that score must be a global variable declared in main.我认为 score 必须是 main 中声明的全局变量。 Show the whole script, but I guess it is the trouble.显示整个脚本,但我想这很麻烦。
Fast answer is: put a return
in start_game()
like this:快速的答案是:像这样在start_game()
中添加一个return
值:
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.如果我有更多时间,我会扩展我的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.