Reputation: 107
I am typing a program for my class, the problem is worded very odd, but I have put in my code for the problem, am I declaring the numbers as float correctly? Simple question but I'm keeping my mind open to other ways of doing things.
print " This program will calculate the unit price (price per oz) of store items,"
print " you will input the weight in lbs and oz, the name and cost."
item_name = (input("Please enter the name of the item. "))
item_lb_price = float(input("Please enter the price per pound of your item "))
item_lbs = float(input("please enter the pounds of your item. "))
item_oz = float(input("plese enter the ounces of your item. "))
unit_price = item_lb_price / 16
total_price = item_lb_price * (item_lbs + item_oz / 16)
print "the total price per oz is", unit_price
print "the total price for", item_name, "is a total of", total_price
Upvotes: 2
Views: 1448
Reputation: 14144
Your floats are fine.
You need to use raw_input
though to get a string back.
input("Enter a number")
is equivalent to eval(raw_input("Enter a number"))
. Currently the code tries to evaluate the input as code (run as a python expression).
15:10:21 ~$ python so18967752.py
This program will calculate the unit price (price per oz) of store items,
you will input the weight in lbs and oz, the name and cost.
Please enter the name of the item. Apple
Traceback (most recent call last):
File "so18967752.py", line 6, in <module>
item_name = (input("Please enter the name of the item. "))
File "<string>", line 1, in <module>
NameError: name 'Apple' is not defined
Other comments:
Slightly cleaned up form:
banner = '''
This program will calculate the unit price (price per oz) of store items.
You will input the weight in lbs and oz, the name and cost.
'''
print banner
item_name = raw_input("Please enter the name of the item. ")
item_lb_price = float(raw_input("Please enter the price per pound of your item."))
item_lbs = float(raw_input("Please enter the pounds of your item."))
item_oz = float(raw_input("plese enter the ounces of your item."))
unit_price = item_lb_price / 16.0
total_price = item_lb_price * (item_lbs + (item_oz / 16.0))
print "The total price per oz is", unit_price
print "The total price for", item_name, "is a total of", total_price
Upvotes: 1
Reputation: 418
if it is python 2 you should be using raw_input instead of input. if it is python 3 you should be enclosing the values you want to print in parentheses.
yes you are using the float function correctly but are not verifying that the input is correct. it is liable and even likely to throw errors.
besides the program is not correct for python 2 because of the input() and not correct for python 3 because of the use of the print statement.
And you should devide by a float: 16.0 instead 16.
Upvotes: 1
Reputation: 1687
Division in python is per default using integers, so if you want a floating point result you should divide by 16.0
unit_price = item_lb_price / 16.0
Upvotes: 0