#πŸ”’ Beginner's exercise error

35 messages Β· Page 1 of 1 (latest)

valid night
#

Just started learning Python. Following a Youtube tutorial and this exercise is giving me the wrong result and I just can't fathom why.

I'm supposed to convert weight from kgs or lbs into the other unit. So I input weight in float, then I input the unit in string, and use an if function to convert it one way or the other.

In app.py, I get the lbs to kg conversion no matter what unit I input.
In app2.py, I get the wrong unit error no matter what unit I input.

nimble tendonBOT
#

@valid night

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.

delicate ether
#

hi

nimble tendonBOT
#
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.")
delicate ether
#

it's

#

it's unit.upper()

grizzled maple
#

!e
next, .upper is the function, you need to call it, i.e.

text = 'hello'
print(text.upper)
print(text.upper())
nimble tendonBOT
valid night
#

Ahhh, okay, okay, all of this is super helpful. Thank you πŸ™‚

delicate ether
#

if you are using the second solution it's

elif unit == "k" or unit == "K"
obsidian marsh
#

Or better:

if unit in ["k", "K"]:
delicate ether
oak pulsar
delicate ether
oak pulsar
delicate ether
#

i mean you are right

#
if ord(unit) == 75 or ord(unit) == 107
wraith karma
#

unit.lower() would be more standard

delicate ether
obsidian marsh
delicate ether
tough dune
nimble tendonBOT
delicate ether
nimble tendonBOT
delicate ether
#

!e a=["K","k","kK","Kk"]; print([(item in "kK" and len(item)==1) for item in a])

nimble tendonBOT
oak pulsar
#

!e unit = "K"; print(unit.lower() == "k")

nimble tendonBOT
nimble tendonBOT
#
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.