1

I'm trying to code a program that will print the total price after a discount. If the price paid is over 100, it will knock off 20%. At the end of the program, I receive this error:

if paid>=100:
    TypeError: unorderable types: str() >= int()

Here is my Code:

paid=input('How much did you pay? ')
if paid>=100:
    actualPay=100*1.25 #20% off total, 100/0.80 = 1.25
    print(actualPay)
0

1 Answer 1

5

Cast the input to an int

paid=int(input('How much did you pay? '))

input always returns a string. You need to explicitly cast it.

Ex:

paid=int(input('How much did you pay? '))
if paid>=100:
    actualPay=100*1.25
    print(actualPay)

A better way would have a try and except block.

try:
    paid=int(input('How much did you pay? '))
    if paid>=100:
        actualPay=100*1.25
        print(actualPay)    
except TypeError:
        print "Please enter a valid input."
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! I don't know how I missed that.
Input does not always return an string. input is Equivalent to eval(raw_input(prompt)). Maybe the input cannot be is converted to\ an integer.
@RobertJacobs Maybe you are using Python2. The OP is using Py3 :). Check the link for the python3 input function

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.