Python Forum
If/Else If Statement Not Running
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If/Else If Statement Not Running
#1
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}')

Attached Files

Thumbnail(s)
   
Reply
#2
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}")
Reply
#3
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.
Reply
#4
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!
Reply
#5
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.
Reply
#6
Ahh, that makes sense. Thanks again!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Stop if statement from running KieranPage 3 1,943 May-05-2020, 04:10 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020