If statement isn't printing [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 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()

Related

startswith() in Python acting unexpectedly [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 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'):

How do I get the it to accept the input (1) and make it print the if statement [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 9 months ago.
Improve this question
def user_input():
user_input = input
name = input("enter your name ")
print("hello "+ name +"!")
print("welcome to gymbros press 1 to continue :")
if input == 1():
print("this is the personal record page where you post your best lifts ")
else:
print("thanks for using gymbros")
####################################
Im not sure what im doing wrong it keeps giving me the, TypeError: 'int' object is not callable
input() return type is str and int is not callable.
user_input is not used
It is recommended that you learn the basic syntax of Python first
name = input("enter your name ")
print("hello", name, "!")
print("welcome to gymbros press 1 to continue :")
if input() == "1":
print("this is the personal record page where you post your best lifts ")
else:
print("thanks for using gymbros")

I'm fairly new to python coding so I'm trying to challenge myself to do elif statements and I can't find how to fix this problem [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 10 months ago.
Improve this question
So here is my code and I can't see what causes the error.
foods = int(input("What is your favorite food?: "))
if foods == apple:
print("you like fruits!")
elif foods == pork:
print("you like meat!")
elif foods == egg:
print("you like poultry!")
The error is
line 3, in <module>
foods = int(input("What is your favorite food?: "))
ValueError: invalid literal for int() with base 10: 'egg:'
Does anyone have any idea how to fix this?
The int part in the int(input("What is your favorite food?: ")) line tries to convert the input string "egg" into an integer, which it canĀ“t. Remove it and the problem will go away.
Also, after the if == or elif ==, you need to write the food type as "egg", "pork", "apple", otherwise python think you are referring to an variable and not a specific string.
Good luck!

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).

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