خرید بک لینک

Vote count: 0

My 12 year old is leaing python scripting. He is writing a very simple code to calculate the discount on a article. Following is the code and it throws the exception on the 3rd line in the code. The exception is:

Traceback (most recent call last): File "C:/Users/basam/AppData/Local/Programs/Python/Python35/discounts.py", line 3, in discount=0.1*price TypeError: can't multiply sequence by non-int of type 'float'

price=input('how much is your item?')
if int(price) <= 10:
discount=0.1*price

Thank you so much

asked 4 mins ago

3 Answers

Vote count: 1

The key is int(price). The input command retus a string which is stored in the variable price.

In the second line, price is converted to a number by the call int(price), but that result is not stored anywhere. It is used for the comparison and then discarded. So when you go to multiply in the third line, you're trying to multiply a number times a string.

answered 46 secs ago

Vote count: 0

Changed to discount=0.1*float(price)

answered 1 min ago

Vote count: -1

You cant multiply an int by a float. You need to cast the price value to a float before multiplying

price = input('how much is your item?')
if int(price) <= 10:
    discount = 0.1 * float(price)

print discount
>> 0.5

Tell him/her to read up on variable types.. in particular the difference between int, double, float

answered 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: دوشنبه 31 خرداد 1395 ساعت: 8:07

صفحه بندی