简体   繁体   English

通过 function 后,我返回的值变为无

[英]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.

相关问题 为什么 python function 仍然返回“无”,尽管我明确地返回了一个值? - why a python function still returns "None" though i am explicitly returning a value? 简单的python函数返回None,并且我正在使用return语句 - Simple python function returning None, AND I am using a return statement 函数返回无而操作返回值 - Function Returning None While Operation Returning Value 系统配置文件 function 在引发异常后变为无 - sys profile function becomes none after exception is raised 我的 python 函数返回 None,即使在声明 return 语句之后也是如此。 我无法理解我的腿在哪里。 递归是强制性的 - My python function is returning None, even if after declaring return statement. I'm unable to understand where I am legging. Recursion is mandatory 我从我的函数返回一个列表,但是当我打印返回的列表时,它打印 NONE - I am returning a list from my function, but when I print that returned list, it prints NONE 递归后Python函数返回None - Python function returning None after recursion Python:从函数返回时没有值 - Python: None value when returning from function 使用SymPy,递归函数返回“无”值 - Using SymPy, recursive function returning “None” value glMultiDrawArraysIndirect function 我怎么会出错? - How am I going wrong in glMultiDrawArraysIndirect function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM