0

I a new in programming , was trying some concepts with if else in python
The if else statement is not working as it should.
I'm using nested if-else , however only included the basic code in the code block
I am using a string as an input and then comparing the input with if else statements.
I tried the code in Thonny ide and it works when I debug the program , but after trying to run the program it does not print anything .
Alternatively if I use an else statement instead of the elif in the end , only the code in the else statement will print



the code is :

new_value = input("enter your choice between left and right")

if new_value =='left':
 print("You chose left")
elif new_value =="right":
 print("you chose right")

4
  • Is it possible you're adding whitespace to the start of your input to make it "appear" correctly in the terminal? That would cause the strings to not be equivalent. Commented Jan 15, 2022 at 14:55
  • Removed the whitespace. It worked . Thank you . can you please explain what happens if the whitespace is kept there . Commented Jan 15, 2022 at 14:59
  • It's simply that the strings aren't equivalent. To a computer, test is not the same as test , because there are use cases where that needs to be the case. To circumvent this when it's not wanted, Python provides methods like str.strip, which removes leading and trailing whitespace. Commented Jan 15, 2022 at 15:03
  • Got it . Thanks a lot mate Commented Jan 15, 2022 at 15:08

1 Answer 1

1

This code is correct.

new_value = input("enter your choice between left and right")
if new_value =="left":
 print("You chose left")
elif new_value =="right":
 print("you chose right")

Alternatively if you use an else statement reffer it,

if new_value =='left':
 print("You chose left")
else:
 print("you chose right")

provide your full nested loop so i will understand problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.