简体   繁体   中英

Python Syntax Error with test code

This is my code:

ItemName = input("Enter your item name: ")
PricePerPound = float(input("Enter the Price per pound: "))
ItemWeightPounds = float(input("Enter the weight of the item in pounds: "))
ItemWeightOunces = float(input("Enter the weight of the item in Ounces: "))
UnitPrice = (PricePerPound/16)
TotalPrice = (PricePerPound * ItemWeightPounds + ItemWeightOunces/16)
print("The Unit Price for your " + ItemName + " is: $" + UnitPrice)
print("The Total Price for your " + ItemName + " is: $" + TotalPrice)

Traceback (most recent call last):
print("The Unit Price for your " + ItemName + " is: $" + UnitPrice)
TypeError: Can't convert 'float' object to str implicitly

whenever I run it in python I get the above error, can anyone identify the issues with my code?

You are closing your print -statement too early.

All you want to print has to be between the brackets:

print("The Unit Price for your " + ItemName + " is: $" + str(UnitPrice))
print("The Total Price for your " + ItemName + " is: $" + str(TotalPrice))

You also need to cast your float - and int -objects to string because you can only concatenate ( + ) string -objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM