Reputation: 37
The instructions are very simple, but I am struggling none the less. We have learned very few things in this class so far, so I am looking for a very simplistic answer.
The instructions are as follows:
Write a Python program that will prompt the user to enter a weight in pounds, then
convert it to kilograms and output the result. Note, one pound is .454 kilograms
What I have so far is :
print("Pounds to Kilos. Please Enter value in pounds.")
x=input('Pounds: ')
float(x)
print(x * .454)
Upvotes: -2
Views: 4032
Reputation: 1
Explanation: First you ask the user's weight using input command, then you can print a message saying converting to kgs.. (it's optional). create a new variable called weight_kgs to convert the user's input to a float, then multiply the input by 0.45. after that convert the weight_kgs variable to a string once again by making a new variable called final_weight, so that you can join the input by user to a string. At the end print a message telling the user's weight. print("you weight is " + final_weight)
```
weight_lbs = input("enter your weight(lbs): ")
print("converting to kgs...")
weight_kgs = float(weight_lbs) * 0.45
final_weight = str(weight_kgs)
print("your weight is " + final_weight + "kgs") # line 5
```
I hope you got it.
Upvotes: 0
Reputation: 2952
Going from your code example I would do something like this.
print("Pounds to Kilos. Please Enter value in pounds.")
x = float(input('Pounds: '))
print(x * 0.454, "kg")
EDIT
Maybe instead of having the calculation in the print()
statement I add a separate variable for it, including the advise about float
from the other solution.
print("Pounds to Kilos. Please Enter value in pounds.")
x = (input('Pounds: '))
kg = float(x) * 0.454
print(kg, "kg")
Upvotes: 0
Reputation: 1304
You are converting the variable x's value to the float
, but not assigning it to anything. So, the real value which x variable holds never changed. You did not edit the x variable in fact. You can try something like this;
print("Pounds to Kilos. Please Enter value in pounds.")
x=float(input('Pounds: '))
print(x * .454)
However, using functions in that nested manner is not recommended. Instead, initialize a new variable to hold the new float-converted value;
print("Pounds to Kilos. Please Enter value in pounds.")
x = input('Pounds: ')
x_float = float(x)
print(x_float * .454)
Upvotes: 1