+ 2
Input with if statements and else
# what's the better way to write this code? Why isn't this proper? Thx sad = input("enter a, b, or c") if sad == 'a': print("typed an a") if sad == 'b': print("typed a b") if sad == 'c': print("typed a c") else: print("typed something else other than a, b, c")
6 odpowiedzi
+ 4
You can do something like
sad = input("enter a, b or c")
if sad in ['a', 'b', 'c']:
print("typed" + sad )
else:
print("typed something else other than a,b,c")
Edit: you should share your actual code for better answers.
+ 3
Why use an if at all, just write:
print("you typed: " + sad)
+ 2
Oh because in my actual code I got a bunch of things going on, its not just a print statement
¯\_(ツ)_/¯
+ 2
Well then we need to see your actual code to deicde, it's hard to think of something complex when the task is easy.
+ 1
a_s_a_p Nesting the if statements can make the code harder to read.
The code on the question isn't good, because if you entered "a" or "b", text "typed something else other than a, b, c" will be printed anyways, because sad == "c" would be False.
0
Ok. Do I write it:
If a or b or c:
If a or b
If a
?
I was just thinking about it.