+ 1

I am working on a very simple text monty python 'game' and am having g issues with else/if

So basically I have choice = input("pick a, b, or c") if choice == ('a'): print ("the text I have written in actual code") elif choice == ('b'): print ("different text in code") elif choice == ('c'): print ("another text") else: print ("snarky comment about the user needing to pick a, b, or c") However, no matter what I input it just prints out the result for inputting ('a'). I am at the point of banging my head into my keyboard. Help me Sol-o-wan Kenobi, you're my only hope! If anyone needs to see a screenshot I can so that but apparently not in solo learn.

22nd Jan 2023, 3:54 PM
Sequoia
Sequoia - avatar
22 odpowiedzi
+ 5
Aah okay, it's clear now with the real code in view First, drop these a = ('A' or 'a') b = ('B' or 'b') c =('C' or 'c') And for the conditionals, use `in` operator for checking inputs if choice in 'Aa': # 'A' or 'a' was chosen elif choice in 'Bb': # 'B' or 'b' was chosen Head or keyboard? hmmm what a tough choice LOL
22nd Jan 2023, 4:43 PM
Ipang
+ 3
Sequoia I see that after your if condition you left out the colon as well as after the elif conditions. the colon is like a code block and the structure of this is similar to a switch in other languages .. if (condition): elif (condition): elif (condition): else: choice = input("pick a, b, or c") if choice == ('a'): print ("the text I have written in actual code") elif choice == ('b'): print ("different text in code") elif choice == ('c'): print ("another text") else: print ("snarky comment about the user needing to pick a, b, or c")
22nd Jan 2023, 4:21 PM
BroFar
BroFar - avatar
+ 3
Oh our good friends here had guided you well ... Stop banging your head onto the keyboard already, you'll have to get a new one tomorrow if you don't 8-)
22nd Jan 2023, 4:27 PM
Ipang
+ 3
Sequoia BTW you don't really need those parentheses in the conditions, you can just write: ``` if choice == 'a': ``` In fact those parentheses just confuse.
23rd Jan 2023, 1:42 AM
Евгений
Евгений - avatar
22nd Jan 2023, 4:35 PM
Lochard
Lochard - avatar
+ 2
Евгений Phil is making a Monty Python joke, which is the same show/ movie that I am working with, that is why and yes it's supposed to be a trick, regardless of the weight, she's a witch, burn her. For context watch the film "Money Python and the search for the Holy Grail. It's a very specific type of humor so it may not be your thing lol. Also, Phil Shary what is the air speed velocity of a laden Swallow?
23rd Jan 2023, 2:02 PM
Sequoia
Sequoia - avatar
+ 1
# Hi, Sequoia ! # The code working as expected. Just do som small adjustments, as add colons in the if statments etc: choice = input("pick a, b, or c: ") print(choice) print() if choice == ('a'): print("the text I have written in actual code") elif choice == ('b'): print ("different text in code") elif choice == ('c'): print("another text") else: print("snarky comment about the user needing to pick a, b, or c")
22nd Jan 2023, 4:16 PM
Per Bratthammar
Per Bratthammar - avatar
+ 1
Okay that was me not paying close enough attention to what i typed in here, I didn't forget the : in the actual code so that is not the issue. I just double checked. I did not include print(choice) print() But after adding it, I do not see any change. It is still printing as if the user input 'A' regardless of what the user inputs...
22nd Jan 2023, 4:25 PM
Sequoia
Sequoia - avatar
+ 1
Ipang a new head or keyboard? ;) I may need both haha.
22nd Jan 2023, 4:30 PM
Sequoia
Sequoia - avatar
+ 1
I figured out the issue. I was using an or statement that I didn't put in here not thinking it was the problem (sheepish smile). Heh. I am still not sure why 'or' broke the program but it did. I had ('a') or ('A') and the same for b and for c incase the user inputted a capital letter instead of a lowercase letter but taking away that option fixed the code! Thanks everyone for your help!
22nd Jan 2023, 4:42 PM
Sequoia
Sequoia - avatar
+ 1
Sequoia The problem is that for every "and" or "or", you have to provide the whole comparision. Your code meant # parentheses are not needed (choice == 'a') or ('A') 'A' is a truthful value in Python, so the first conditional statement will always be evaluated as True.
22nd Jan 2023, 4:48 PM
Lochard
Lochard - avatar
+ 1
duck_weight_min = 760 duck_weight_min = 1600 input (weight) if weight >= ('duck_weight_min'): or if weight <= ('duck_weight_max') print ("She's a witch. Burn her!")
23rd Jan 2023, 1:41 PM
Phil Shary
Phil Shary - avatar
+ 1
Sequoia OK I see now. I wouldn't guess that without an explanation. 👍
23rd Jan 2023, 2:06 PM
Евгений
Евгений - avatar
+ 1
Try this method choice = input() if choice == ("a"): print ("the text I have written in actual code") elif choice == ("b"): print ("different text in code") elif choice == ("c"): print ("another text") else: print ("snarky comment about the user needing to pick a, b, or c")
23rd Jan 2023, 4:18 PM
Hussin Aourir
Hussin Aourir - avatar
+ 1
Sequoia When you need help with some code, never make changes to it, otherwise people will be dealing with some _other_ code. Even a small change can result in a completely different behavior. Or if you make any changes make sure first that the behavior isn't affected. Also use Code Playground to share the code you are referring to in questions/answers, not pictures and plain text inside a question. It is very easy to use and very helpful for others to inspect/experiment with your code. The easier the way for others to help you the more likely you'll get a helpful response.
23rd Jan 2023, 5:39 PM
Евгений
Евгений - avatar
+ 1
Sequoia As for the initial problem, the cause is the operation priority in the condition. The condition is evaluated as: (choice == 'a') or 'A' While the left part of "or" can be false, the right part is always True since non-empty strings ('A') are truthy values. So the condition always evaluates to True. Actually it evaluates to 'A' which is True in these conditions or just to True (self example below for explanation). So the first condition in the "if" statement always holds true. ``` choice = 'a' cond = (choice == 'a' or 'A') print(cond, bool(cond)) choice = 'b' cond = (choice == 'a' or 'A') print(cond, bool(cond)) ``` This will print ``` True True A True ``` https://code.sololearn.com/c9v3F9br8AE6/?ref=app
23rd Jan 2023, 6:18 PM
Евгений
Евгений - avatar
+ 1
Abdul rahmon "choice.isalpha()" is redundant here.
24th Jan 2023, 11:18 AM
Евгений
Евгений - avatar
0
Here is a link to an image of the ACTUAL code on my computer if anyone would like to see it. That way I do not miss something like the : when positing it on my phone heh. https://ibb.co/mSD9X6c
22nd Jan 2023, 4:29 PM
Sequoia
Sequoia - avatar
0
# Created by medokan choice = input("pick a, b, or c") for x in choice : if x == ('a'): print ("'a' the text I have written in actual code") elif x == ('b'): print ("'b' different text in code") elif x == ('c'): print ("'c' another text") else: print ("snarky comment about the user needing to pick a, b, or c")
22nd Jan 2023, 7:14 PM
Medo Kan
Medo Kan - avatar