打破while循环功能?

打破while循环功能?

问题描述:

我试图做一个函数,其中有一个if/elif语句,并且我想要if打破一个while循环。该函数用于文本冒险游戏,并且是/否问题。以下是我想出了迄今为止..打破while循环功能?

def yn(x, f, g): 
    if (x) == 'y': 
     print (f) 
     break 
    elif (x) == 'n' 
     print (g) 

name = raw_input('What is your name, adventurer? ') 
print 'Nice to meet you, '+name+'. Are you ready for your adventure?' 

while True: 
    ready = raw_input('y/n ') 
    yn(ready, 'Good, let\'s start our adventure!', 
     'That is a real shame.. Maybe next time') 

现在我不知道如果我使用的功能权限,但是当我尝试它,它说我不能有突破功能。所以如果有人可以帮助我解决这个问题,并且如果你可以帮助我,如果函数和调用函数本身的格式不对,那将非常感激。

+1

您的意思是让函数内的行缩进吗? – 2013-04-18 02:14:12

+0

我更新了我的解释,如果您有任何其他问题,请随时捅我。 – eandersson 2013-04-19 09:49:32

一种方法是让yn返回一个布尔值,然后用它来摆脱循环。否则,函数中的break不能跳出调用函数中的循环。

def yn(x, f, g): 
    if (x) == 'y': 
     print (f) 
     return True 
    elif (x) == 'n' 
     print (g) 
     return False 

name = raw_input('What is your name, adventurer? ') 
print 'Nice to meet you, '+name+'. Are you ready for your adventure?' 
done = False 
while not done: 
    ready = raw_input('y/n ') 
    done = yn(ready, 'Good, let\'s start our adventure!', 'That is a real shame.. Maybe next time') 

使用break,即使未满足循环结束的条件,也可以退出循环。你不能休息,因为'if/elif'不是一个循环,它只是一个条件语句。

您需要跳出循环内部的while循环,而不是从另一个函数中跳出。

像下面可能接近你想要什么:

def yn(x, f, g): 
    if (x) == 'y': 
     print (f) 
     return 0 
    elif (x) == 'n': 
     print (g) 
     return 1 

name = raw_input('What is your name, adventurer? ') 
print 'Nice to meet you, '+name+'. Are you ready for your adventure?' 

while True: 
    ready = raw_input('y/n: ') 
    if (yn(ready, 'Good, let\'s start our adventure!', 'That is a real shame.. Maybe next time')): 
     break 
+0

你能解释一下添加返回0和返回1是否有效吗?我只是看着它而不太明白。 – 2013-04-18 02:32:33

+1

@Duane Moore您应该将其改为'return True'并'return False'以提高可读性。 – 2013-04-18 02:42:34

你可以用一个异常工作:

class AdventureDone(Exception): pass 

def yn(x, f, g): 
    if x == 'y': 
     print(f) 
    elif x == 'n': 
     print(g) 
     raise AdventureDone 

name = raw_input('What is your name, adventurer? ') 
print 'Nice to meet you, '+name+'. Are you ready for your adventure?' 

try: 
    while True: 
     ready = raw_input('y/n ') 
     yn(ready, "Good, let's start our adventure!", 
      'That is a real shame.. Maybe next time') 
except AdventureDone: 
    pass 
    # or print "Goodbye." if you want 

这一遍又一遍地循环的while循环,但里面的yn()函数会产生一个打破循环的异常。为了不打印回溯,必须捕获并处理异常。

+0

创造性的答案,但在这种情况下打破while循环的唯一方法是按'n'。我假设他想在输入'y'后继续游戏。 – eandersson 2013-04-19 14:16:15

您将需要将函数中的中断更改为返回,并且在用户未向您提供正确输入的情况下,您需要有一个else语句。最后,您需要将您的while loop中的电话转为if语句。

如果玩家输入所需的命令,这将允许您打破while语句,如果没有,它会再次提问。我还更新了您的yn函数,以允许用户使用小写字母和大写字符,以及是和否。

def yn(input, yes, no): 
    input = input.lower() 
    if input == 'y' or input == 'yes': 
     print (yes) 
     return 1 
    elif input == 'n' or input == 'no': 
     print (no) 
     return 2 
    else: 
     return 0 

name = raw_input('What is your name, adventurer? ') 
print 'Nice to meet you, %s. Are you ready for your adventure?' % name 

while True: 
    ready = raw_input('y/n ') 
    if yn(ready, 'Good, let\'s start our adventure!', 
     'That is a real shame.. Maybe next time') > 0: 
     break 

这背后的想法很简单。 yn函数有三个状态。用户回答是,不是或无效。如果用户的回答是“是”或“否”,则函数将返回1表示“是”,而2表示“否”。如果用户不提供有效的输入,例如空白空间,它会返回0

里面的while True:循环中,我们包裹YN(“......‘’......”)函数与if statement来检查,如果该yn函数返回一个数字大于0.因为yn将返回0,如果用户向我们提供了有效输入,并且1或2为有效输入。

一旦我们从yn得到有效答复,我们称之为休息,即停止while loop,我们就完成了。

+0

我没有看到其他的巨大差异,已经7小时的答案,使'yn()'返回一个布尔类似的值... – glglgl 2013-04-18 09:47:45

+0

我告诉瑞恩,当我醒来时,我会给他一个答案。我遵守我的承诺,并且通过添加'else'语句并使其不区分大小写来处理它。 http://stackoverflow.com/questions/16071394/cant-get-a-function-to-work/16071446#16071446 – eandersson 2013-04-18 09:53:35

+1

啊,'else'部分确实是非常好的东西。 – glglgl 2013-04-18 09:54:44

a = True 

def b(): 
    if input("") == "Quit": 
     global a 
     a == False 
    else: 
     pass 

while a == True: 
    print('Solution') 
+0

请解释如何回答这个问题。 – jpaugh 2017-08-01 21:03:38

+0

如果在while循环中调用if语句,它将打破while循环。 – 2017-08-02 09:50:10