0

Is there a way to write this in a easier way than putting elif a thousand times?

travel = input("Where do you want to spend your vacation? \n") if travel == "Home": print("Lazy ass") elif travel == "France": print("For romance?") elif travel == ("California"): print("With the celebrities?") elif travel == "Hawaii": print("Beautiful view :)")

8th Jul 2017, 1:25 PM
Kik Me: SheDrippinWet
Kik Me: SheDrippinWet - avatar
12 ответов
+ 5
Since Python doesn't have a Select statement or Switch, your only other option would be to use a Dictionary. I don't know if it would look much prettier, but sometimes you don't have a lot of choices.
8th Jul 2017, 1:28 PM
Jim Tully
Jim Tully - avatar
+ 4
""" Totally agree with @Sapphire, but you can bypass the external file and define dict with litterals directly in your code (but the most efficient way for a lot of data is to externalize them): """ travels = { "Home": "Lazy ass", "France": "For romance?", "California": "With the celebrities?", "Hawaii": "Beautiful view :)" } travel = input("Where do you want to spend your vacation? \n") if travel in travels.keys(): print(travels[travel]) else: print('Unknown destination!')
8th Jul 2017, 3:13 PM
visph
visph - avatar
+ 3
Python does not have a switch statement?? Wow....
8th Jul 2017, 2:05 PM
Karl T.
Karl T. - avatar
+ 3
Belong to C optimization? Python is slow as heck. :P
8th Jul 2017, 3:16 PM
Karl T.
Karl T. - avatar
+ 2
It doesn't have 'switch' statements because Python doesn't need it. switch is an old concept that originally belonged to C as a compiler optimization, modern compilers do not need this to handle logic statements. Back to topic: In this case, it is redundant to write a thousand if statements. The better option would be to have these 'vacation' spots in a document, such as excel or text. Then, import this into python, and have Python place these into a dictionary. So instead of 1000 if statements, you now have roughly 1. Ta da
8th Jul 2017, 2:58 PM
Sapphire
+ 2
python is a high level language, where as c++ is a lower level language. It means python is a higher level of abstraction than c++. So it looks almost like English to us, humans. C++ is closer to C, and then machine code. It's faster than python, buuuuuuut... it's much more bare bones than python. C/c++ have more control over the performance of a computer than python, but python can build a program faster than both of them, thanks to its high level abstraction and built in functions. Your friend can laugh at Python for being 'easy', but Python can build a program faster than C or C++ could ever hope too. hehe ^^ They're both good at their own specific areas.
8th Jul 2017, 6:04 PM
Sapphire
+ 2
Some say that C++ is more bare bones, but that is just because they don't know or are not aware of libraries that are available. The STL for example is a highly optimized lib that you can use for all kinds of data structures.
8th Jul 2017, 6:17 PM
Karl T.
Karl T. - avatar
0
Okay appreciate it
8th Jul 2017, 1:29 PM
Kik Me: SheDrippinWet
Kik Me: SheDrippinWet - avatar
0
Thank you all for your answers 🤓
8th Jul 2017, 5:44 PM
Kik Me: SheDrippinWet
Kik Me: SheDrippinWet - avatar
0
Okay gotcha. Thanks @Sapphire
8th Jul 2017, 6:49 PM
Kik Me: SheDrippinWet
Kik Me: SheDrippinWet - avatar
- 1
I'm stuck on Python.. So I'd like to master it first and coding is fun as heck. I love to probem and it's like, a part of me lol.
8th Jul 2017, 9:09 PM
Kik Me: SheDrippinWet
Kik Me: SheDrippinWet - avatar
- 2
One of my friends laughed at me because I was writing "Words" on Python. Why is that? He told me to learn C++ before Python.
8th Jul 2017, 5:45 PM
Kik Me: SheDrippinWet
Kik Me: SheDrippinWet - avatar