+ 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")

26th Feb 2020, 11:50 PM
sudo-asap
sudo-asap - avatar
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.
27th Feb 2020, 1:05 AM
Utkarsh Sharma
Utkarsh Sharma - avatar
+ 3
Why use an if at all, just write: print("you typed: " + sad)
27th Feb 2020, 12:00 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
Oh because in my actual code I got a bunch of things going on, its not just a print statement ¯\_(ツ)_/¯
27th Feb 2020, 12:16 AM
sudo-asap
sudo-asap - avatar
+ 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.
27th Feb 2020, 12:17 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 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.
27th Feb 2020, 3:12 PM
Seb TheS
Seb TheS - avatar
0
Ok. Do I write it: If a or b or c: If a or b If a ? I was just thinking about it.
26th Feb 2020, 11:53 PM
sudo-asap
sudo-asap - avatar