2

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
3
  • Are you using Python 2 or 3? Commented Sep 23, 2013 at 19:59
  • 1
    Probably want decimal.Decimal instead of float when dealing with money. Commented Sep 23, 2013 at 20:04
  • 1
    In python we don't declare variables. There just isn't such a thing. What you are doing is binding names to values. Commented Sep 23, 2013 at 20:11

3 Answers 3

1

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:

  1. For your multiline banner at the top, declare the string as a multiline string and print that.
  2. Check to make sure the ranges make sense for the inputs (validate). Of course, if this is for a class you may not have reached this point (Seen if/else?)
  3. Be explicit with your constants to make them floats to make sure it doesn't default to integer division

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
Sign up to request clarification or add additional context in comments.

2 Comments

With Python 2, which is albeit still a common scenario.
very nice, no we haven't hit else/if in class yet but that cleaned up code really clears up my questions
1

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.

Comments

0

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

1 Comment

He's using the float() function on all of his inputs, so that is redundant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.