循环回到代码中的特定点

时间:2014-12-08 18:59:22

标签: python

所以我正在编写一个小游戏,我正在尝试做一些我不明白该怎么做的事情。我已经定义了一个函数,当代码工作的条件都没有时,我希望它返回到另一行代码。但我不明白怎么做。这是我正在努力的代码的一部分

print "What's your favourite type of pokemon?"

fav_type = raw_input()

def chosen_pokemon():
    if "fire" in fav_type:
         print "So you like fire types? I'll give you a chimchar!"
    elif "water" in fav_type:
         print "So you like water types? I'll give you a piplup!"
    elif "grass" in fav_type:
         print "So you like grass types? I'll give you a turtwig!"
    else:
         print "sorry, you can only choose grass, water or fire"
         start()

chosen_pokemon()

就像我希望代码回到“你最喜欢的哪种口袋妖怪?”如果它最终由其他人,但如何?在回答时请尽可能清楚,因为我刚刚开始学习编程,而我几乎一无所知

4 个答案:

答案 0 :(得分:2)

您可以将raw_input步骤置于while循环中,只有在收到有效响应时才退出。我添加了几条评论来解释发生了什么。

def chosen_pokemon():
    while True: # repeat forever unless it reaches "break" or "return"
        print "What's your favourite type of pokemon?"
        fav_type = raw_input()
        if "fire" in fav_type:
             print "So you like fire types? I'll give you a chimchar!"
        elif "water" in fav_type:
             print "So you like water types? I'll give you a piplup!"
        elif "grass" in fav_type:
             print "So you like grass types? I'll give you a turtwig!"
        else:
             print "sorry, you can only choose grass, water or fire"
             continue # jumps back to the "while True:" line
        return # finished; exit the function.

chosen_pokemon()

输出:

>>> chosen_pokemon()
What's your favourite type of pokemon?
yellow
sorry, you can only choose grass, water or fire
What's your favourite type of pokemon?
grass
So you like grass types? I'll give you a turtwig!
>>> 

这是关于while循环的教程。 http://www.tutorialspoint.com/python/python_while_loop.htm

答案 1 :(得分:1)

def chosen_pokemon(inp):
    if "fire" in inp:
        print "So you like fire types? I'll give you a chimchar!"
        return True
    elif "water" in inp:
        print "So you like water types? I'll give you a piplup!"
        return True
    elif "grass" in inp:
        print "So you like grass types? I'll give you a turtwig!"
        return True
    else:
        print "sorry, you can only choose grass, water or fire"
        return False

print "What's your favourite type of pokemon?"
while(True):
   if(chosen_pokemon(raw_input())):
       break 

修改 您还可以将您的回复放在字典中,以避免使用if-else-clause;

print "What's your favourite type of pokemon?"

dic = {"fire":"So you like fire types? I'll give you a chimchar!",  \
       "water":"So you like water types? I'll give you a piplup!",  \
       "grass":"So you like grass types? I'll give you a turtwig!"}

while(True):
    d = raw_input()
    if d in dic:
        print dic[d]
        break
    else:
        print "sorry, you can only choose grass, water or fire"

答案 2 :(得分:0)

你只需要将你拥有的东西放到另一个返回顶部的循环中,然后再次询问问题,直到收到有效的答案。

答案 3 :(得分:0)

我是Python的新手。而不是使用循环,我使用递归来解决这个问题,在 else 语句

块中调用 chosen_pokemon ()本身
def chosen_pokemon():
    print "What's your favourite type of pokemon?"

    fav_type = raw_input()
    if "fire" in fav_type:
         print "So you like fire types? I'll give you a chimchar!"
    elif "water" in fav_type:
         print "So you like water types? I'll give you a piplup!"
    elif "grass" in fav_type:
         print "So you like grass types? I'll give you a turtwig!"
    else:
         print "sorry, you can only choose grass, water or fire. Try again"
         chosen_pokemon()

chosen_pokemon()

我希望这个解决方案可以帮助你们。