Python Forum
If/Else If Statement Not Running - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: If/Else If Statement Not Running (/thread-35742.html)



If/Else If Statement Not Running - new_coder_231013 - Dec-08-2021

Hi,

This is my third post on any sort of coding website and I'm an absolute beginner at programming. I'm going through an online Python tutorial and am working on an exercise that involves writing code to convert the user's weight (input by the user) from pounds to kilograms and vice versa. The user input code I wrote is running without errors but the if and else if statements to convert the weight are not working. I've looked at the solution in the tutorial and can't discern any difference between what I wrote and the answer.

I've provided the code below and also attached a screenshot from PyCharm showing the code and the output that I'm getting. Any help would be greatly appreciated. Thanks!

user_weight = int(input('What is your weight? '))
pounds_or_kilograms = input('Did you enter your weight in pounds or kilograms? ')
if pounds_or_kilograms.lower == "pounds":
     weight_in_kilograms = (user_weight * 0.45359237)
     print('Your weight in kilograms is {weight_in_kilograms}')
elif pounds_or_kilograms.lower == "kilograms":
     weight_in_pounds = (user.weight * 2.2)
     print('Your weight in pounds is {weight_in_pounds}')



RE: If/Else If Statement Not Running - Larz60+ - Dec-08-2021

There are a couple of things wrong.
  1. lower should be lower(), you want to execute lower, without () you get the address of the function lower
  2. your print statements use f-string, you forgot the 'f'

user_weight = int(input("What is your weight? "))
pounds_or_kilograms = input("Did you enter your weight in pounds or kilograms? ")

if pounds_or_kilograms.lower() == "pounds":
    weight_in_kilograms = user_weight * 0.45359237
    print(f"Your weight in kilograms is {weight_in_kilograms}")
elif pounds_or_kilograms.lower() == "kilograms":
    weight_in_pounds = user.weight * 2.2
    print(f"Your weight in pounds is {weight_in_pounds}")



RE: If/Else If Statement Not Running - deanhystad - Dec-08-2021

You process the weight input right away on the input line. You should do the same with the units.
weight = int(input("What is your weight? "))
if input("Did you enter your weight in kilograms (k) or pounds (p)? ").lower()[1] == "k":
    print(f"Your weight in pounds is {weight * 2.2}")
else:
    print(f"Your weight in kilograms is {weight * 0.45359237}")
I don't like typing, so I give the user a shortcut on selecting the weight units. Since the only units I support are kilograms or pounds my program assumes the units are kilograms or pounds. Your program assumed kilograms, pounds or other where other didn't do any conversion.

Ther is no need for a variable to hold the result of the conversion. The only thing you do with the value is print it, so why not put the equation directly in the print statement? There is also no reason to use a variable for storing the units. The unit value is only used once, so I rolled the input() command into the if statement.


RE: If/Else If Statement Not Running - new_coder_231013 - Dec-09-2021

Thanks to both of you. Two follow-up questions.

1 ) I'm getting an error message (see below) when I try to run the code deanhystad suggested. The error message has a caret symbol (^) pointing to the quotes at the end. Do either of you know why this is happening? I'm not sure if it has to do with the fact that my PyCharm is apparently using Python 3.4.

Line 3
print(f"Your weight in pounds is {weight * 2.2}")

SyntaxError: invalid syntax

2 ) Deanhystad, could you please explain what the [1] is or does in .lower()[1]? Is this simply a default value (and not necessary for the code to run correctly)?

Thanks again!


RE: If/Else If Statement Not Running - deanhystad - Dec-09-2021

Sorry, [1] should be [0] and it selects the first letter you typed. I can see why selecting the second letter was confusing.

You should not be getting an error with the print statement. I just ran the posted code. After changing [01] to [1] it runs correctly.


RE: If/Else If Statement Not Running - new_coder_231013 - Dec-10-2021

Ahh, that makes sense. Thanks again!