I'm building a program to calculate the shipping costs for a parcel. I would like to give the user the opportunity to input the weight of the parcel using the input() function.
I would like the program to return "Invalid input" if anything but an integer or float is inputted. I've tried using the following code:
weight = input("How heavy is the package you want to ship? ")
while type(weight) == float or int:
if weight <= 2:
price_per_pound = 1.50
elif weight <= 6:
price_per_pound = 3.00
else:
price_per_pound = 4.00
break
else:
print("Invalid input")
However, I get an error saying '<=' not supported between instances of 'str' and 'int'. I know that in Python 3 input() always returns a string, so I've also tried this:
weight = float(input("How heavy is the package you want to ship? "))
But if the user inputs a character, it returns the error "could not convert string to float".
I'm pretty new to programming, any help would be appreciated. Thanks!