python - breaking while loop with function? -
i trying make function has if/elif statement in it, , want if break while loop.. function text adventure game, , yes/no question. here have come far..
def yn(x, f, g): if (x) == 'y': print (f) break elif (x) == 'n' print (g) name = raw_input('what name, adventurer? ') print 'nice meet you, '+name+'. ready adventure?' while true: ready = raw_input('y/n ') yn(ready, 'good, let\'s start our adventure!', 'that real shame.. maybe next time')
now i'm not sure if using function right, when try out, says can't have break in function. if me problem, , if me if function , calling function formatted wrong, appreciated.
you can work exception:
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 name, adventurer? ') print 'nice meet you, '+name+'. ready adventure?' try: while true: ready = raw_input('y/n ') yn(ready, "good, let's start our adventure!", 'that real shame.. maybe next time') except adventuredone: pass # or print "goodbye." if want
this loops while
loop on , over, inside yn()
function exception raised breaks loop. in order not print traceback, exception must caught , processed.
Comments
Post a Comment