startswith() in Python acting unexpectedly [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
I have the following simple Python code that support to ask if a player wants to play the game again or not.
print('Do you want to play again? (yes or no)')
if not input('> '.lower().startswith('y')):
print('no')
else:
print('yes')
However, it does not work correctly. When I run it, it prints a "False" out of nowhere. And regardless if I enter "yes" or "no", the outcome is always "yes".
Do you want to play again? (yes or no)
Falseyes
yes
Do you want to play again? (yes or no)
Falseno
yes
But the following code works.
print('Do you want to play again? (yes or no)')
again = input('> '.lower())
print(again)
again = again.startswith('y')
print(again)
results:
Do you want to play again? (yes or no)
> yes
yes
True
Do you want to play again? (yes or no)
> no
no
False

Your brackets are in the wrong position. You want to call .lower().startswith('y') on the result of input('> ').
Change this:
if not input('> '.lower().startswith('y')):
to:
if not input('> ').lower().startswith('y'):

Related

The below block of code doesn't give the desired output in python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 days ago.
Improve this question
i=input("left ot right?").lower()
if i=="l" :
i2=input("kitchen or bedroom").lower()
if i2=="Bedroom":
i3=input("Under the Bed or cupboard or balcony? ").lower()
if i3=="cupboard":
print("Those are cloths!!GAME OVER!!")
elif i3=="Balcony" :
print("Views good ,but no treat ,GAME OVER!!")
elif i3=="Bed" :
print("YOU WIN")
else:
print("game over")
else:
print("oops no noa:(,GAME OVER!!")
else:
print("Wrong room,GAME OVER!!")
When i execute this lines of code this doesn't give the right output
if i2=="Bedroom":
i3=input("Under the Bed or cupboard or balcony? ").lower()
When i type this line the output is expected to take input but it executes the 3rd last line of the code
the problem here is that you have used the lower function with the input value. So the string you get is actually the lowercase of the inputted string. The if statement will return False because 'bedroom' is not equal to 'Bedroom', that's why your code skips to the else part.
Hope this helps you understand and fix your problem.

For some reason, despite the condition of this while loop being False, the loop is not terminating [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 months ago.
Improve this question
What I am trying to do is make my code re-executable by placing it all within a while loop and asking the user if they wish to continue. But, for whatever reason, despite start not being 'y', the code continues on. I tried messing around with it but nothing seems to work. Its a really simple problem but I can't put my hand on what's going on. Here is the problematic code:
start = "y"
while start == "y":
holder = input("Create a file? (y/n): ")
while holder != "y" and holder != "n":
print("ERROR: Only enter 'y' or 'n'.")
print()
holder = input("Create a file? (y/n): ")
start = holder
BTW, yes, I know I could use break but I just really want to know what is wrong with this.
While you don't enter "y", or "n", the code stays in the while loop. The code is perfect however. You might also be giving input typos.
This is a simpler design:
while True:
doit = input("Create a file? (y/n): ")
if doit in 'Nn':
break
if doit not in 'Yy':
continue
# Go create the file.

Why doesn't input return a the print value with .lower() or .upper() commands? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
When i run the code my python doesn't recognize what i put in whether it's with .lower or .upper, why is that?
import sys
Good = input('Am i Good? > ').upper()
if Good == 'no':
print(True, 'You are good')
elif Good == 'no':
print(True, ' You are still good')
elif Good == 'quit':
sys.exit()
In this line of code:
Good = input('Am i Good? > ').upper()
You transform the input to uppercase, but then you compare the string with lowercase strings ("no", "quit"). This will never match since "NO" and "no" are different strings (Python cares about case when comparing strings).

If statement isn't printing [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm a beginner in python and coding in general.
I was messing around with if statements and whenever I ran this code, it would never print what I asked it to, it would only print what was in the else statement.
I'm putting my code down below and if someone could help me understand the issue that'd be great, thanks!
canyou = input("Can you make a good meal on your own? ")
canyou1 = input("Can you wash your clothes well? ")
canyou2 = input("Can you clean the house properly? ")
if canyou.upper == "no" and canyou1.upper == "no" and canyou2.upper == "no":
print("You need your mama")
else:
print("You don't need your mama")
You're comparing the uppercase string to a lowercase example. To fix this you can do the following
if canyou.lower() == "no" and canyou1.lower() == "no" and canyou2.lower() == "no":
print("You need your mama")
else:
print("You don't need your mama")
You also need to call the method by adding parenthesis after upper/lower, as shown in my example.
You need to call the method of each string like this:
canyou = input("Can you make a good meal on your own? ")
canyou1 = input("Can you wash your clothes well? ")
canyou2 = input("Can you clean the house properly? ")
if canyou.lower() == "no" and canyou1.lower() == "no" and canyou2.lower() == "no":
print("You need your mama")
else:
print("You don't need your mama")
Also you need to call .lower()

Invalid syntax with else statement in python3 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
When I try to run my code it says I have invalid syntax with my else statement, but I can't figure out what's wrong.
import random
import time
username = input("Hello. Please enter your name, then press 'enter'.
After you type something, you will need to /n"
"click the 'enter' key to send it")
print ("Hello " + username)
time.sleep(3)
game_tutorial_input = input("Do you wish to see the tutorial? (y/n)")
if game_tutorial_input == "y":
print ("Great! Press enter after each instruction to move /n"
"onto the next one.")
else
print("Are you sure? (y/n")
indent matters in python , make sure the else is on the same indent column as the if. and as stated you need a : at the end of else.. ie, else:
Try 'else:'
I believe it's missing a colon.
indent out the else
add a colon to end of the else
Explanation:
else, if, elif, with etc.. are statements which need colons after the statement
add colon after else and indentation of else is wrong
if game_tutorial_input == "y":
print ("Great! Press enter after each instruction to move /n"
"onto the next one.")
else:
print("Are you sure? (y/n")

Categories