#πŸ”’ My If-else statement ignores my input

33 messages Β· Page 1 of 1 (latest)

olive pollen
#

Whenever I type "m" to try and get it to switch to the m block it just goes with days anyway. I'm doing this for my coding class and I KNOW that a lot of this is probably really repetitive code but this is my main issue. I am trying to get it to where if I type "d" or "D" it goes to the day block and if I type "m" or "M" then it goes with months. Please help

barren valveBOT
#

@olive pollen

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

void relic
#

!or

barren valveBOT
#
The or-gotcha

When checking if something is equal to one thing or another, you might think that this is possible:

# Incorrect...
if favorite_fruit == 'grapefruit' or 'lemon':
    print("That's a weird favorite fruit to have.")

While this makes sense in English, it may not behave the way you would expect. In Python, you should have complete instructions on both sides of the logical operator.

So, if you want to check if something is equal to one thing or another, there are two common ways:

# Like this...
if favorite_fruit == 'grapefruit' or favorite_fruit == 'lemon':
    print("That's a weird favorite fruit to have.")

# ...or like this.
if favorite_fruit in ('grapefruit', 'lemon'):
    print("That's a weird favorite fruit to have.")
rich tide
#

You can just do input(...) instead of (input(...))

#

I assume you were probably casting that input to something and forgot to remove the parens

low agate
#

Although instead of payment == "d" or payment=="D"

#

You can use

#

If payment.lower() == "d"

raven cradle
#

And using the same text in the input prompt as the print statement after it, seems redundant.

olive pollen
#

I know that I just don't know any other way

low agate
#

Just remove the print statement that repeats the prompt

olive pollen
#

Professor wants me to print the question and the input next to that but i also want the question to appear for the input up top

low agate
#

The question should appear when you call input()

#

Then the user types the answer on the same line and hits enter

olive pollen
#

What would a line like that look like?

low agate
#

Getting input with a prompt message? You already have it on line 3

#

Also you already are getting the payment period using a input prompt

olive pollen
#

lemme send his example rq

#

Bold is the input

low agate
#

Yeah you can do that with x = float(input("enter float: ")

kindred fulcrum
#

whats your output?

low agate
#

That should show in the console as enter float: 6

raven cradle
#
>>> loan_amount = float(input("What is the loan amount? "))
What is the loan amount? 350.00
low agate
#

See, no need for an extra print this

kindred fulcrum
#

should be

if repayment_period == "d" or repayment_period == "D":```
raven cradle
kindred fulcrum
#

or make the input .lower() seems the most logical

raven cradle
#

although you could also just do

if repayment_period.casefold() == "d":
#

If you're comparing, you should use casefold. lower should be used for when you need to display lowercase.

barren valveBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.