Home Python Text Loop(solved)

# Python Text Loop(solved)

SenpaiPuppy
1#
SenpaiPuppy Published in 2018-01-11 02:59:11Z
 What I’m trying to do is when it gets to else and prints error I want it to loop back to choice1. choice1 = input("Were would " + name + " like to go?\nThe Kitchen\nThe Couch\nOutside") if choice1 == "The Kitchen": choices.append("The Kitchen") print(name + " walked towards The Kitchen.") elif choice1 == "The Couch": choices.append("The Couch") print(name + " went and had sat on The Couch.") elif choice1 == "Outside": choices.append("Outside") print(name + " put on their pack and went out the door.") else: print("error")  If there’s an error/it reaches else I want it to loop back to choice1.
Olivier Pons
2#
Olivier Pons Reply to 2018-01-11 08:37:40Z
 I'll try my best to answer the pythonic way (even though I dont consider myself Python expert): Python is so great with lists and dictionnaries that you can avoid things like switch/case stuff. Here's your comparison the Python way: name = "Olivier" possible_choices = { "The Kitchen": lambda name: "{} walked towards The Kitchen.".format(name), "The Couch": lambda name: "{} went and had sat on The Couch.".format(name), "Outside": lambda name: "{} put on their pack and went out the door.".format(name), } while True: choice = input("Were would {} like to go?\n{}\n>".format( name, '\n'.join(possible_choices))) if choice in possible_choices: print(possible_choices[choice](name)) break; # break the loop print("error") # loops  With that: you have no "switch/case" because Python makes it possible, if you just add a key + lambda everything will work it's shorter thus: it's easier to read it's easier to maintain it's easier to understand thus it'll cost less to your company in the long run Learn how to use lambdas (= anonymous functions) which are used in every modern language, and this "if choice in possible_choices" is so clear that it makes it almost English!
guichao
3#
 while True: choice1 = raw_input("Were would " + name + " like to go?\nThe Kitchen\nThe Couch\nOutside") if choice1 == "The Kitchen": choices.append("The Kitchen") print(name + " walked towards The Kitchen.") break elif choice1 == "The Couch": choices.append("The Couch") print(name + " went and had sat on The Couch.") break elif choice1 == "Outside": choices.append("Outside") print(name + " put on their pack and went out the door.") break else: print("error") continue 
 You could use a while loop and have the if/elif statements make it escape the loop. Like this: a = True while a: choice1 = input("Were would " + name + " like to go?\nThe Kitchen\nThe Couch\nOutside") if choice1 == "The Kitchen": choices.append("The Kitchen") print(name + " walked towards The Kitchen.") a = False elif choice1 == "The Couch": choices.append("The Couch") print(name + " went and had sat on The Couch.") a = False elif choice1 == "Outside": choices.append("Outside") print(name + " put on their pack and went out the door.") a = False else: print("error")  a is a boolean statement set to true. The script enters a while loop that runs while a == True. All of the if/elif statements make a false, causing the loop to end.