Reading user input in Python

Question:

Write nested decision structures that perform the following: If amount1 is greater than 10 and amount2 is less than 100, display the greater of amount1 and amount2.

this is what I have so far:

amount1=print('Enter amount1:')
amount2=print('Enter amount2:')
if amount1> 10 and amount2< 100:
    if amount1>amount2:
        print('amount1 is greater')
    elif amount2>amount1:
        print('amount2 is greater')
else:
    print('Amounts not in valid range')

when I run the program, this error message comes up:

Traceback (most recent call last):
  File "/Users/Yun/Documents/untitled", line 3, in <module>
    if amount1> 10 and amount2< 100:
TypeError: unorderable types: NoneType() > int()
Asked By: user3307366

||

Answers:

The print() function returns None, which you store in amount1 and amount2. You probably meant to use input() there instead:

amount1 = input('Enter amount1:')
amount2 = input('Enter amount2:')
Answered By: Martijn Pieters

Did you mean

amount1=raw_input('Enter amount1:')
amount2=raw_input('Enter amount2:')

if amount1> 10 and amount2< 100:
    if amount1>amount2:
        print('amount1 is greater')
    elif amount2>amount1:
        print('amount2 is greater')
else:
    print('Amounts not in valid range')
Answered By: akaRem
  1. You need to use input instead of the print function.
  2. You need to cast the variables amount1 and amount2 as an int data type.
    amount1=int(input('Enter amount1:'))
    amount2=int(input('Enter amount2:'))
    if amount1> 10 and amount2< 100:
        if amount1>amount2:
            print('amount1 is greater')
        elif amount2>amount1:
            print('amount2 is greater')
    else:
        print('Amounts not in valid range')
Answered By: Mesgana Desta

Print() function returns str type value, you need to convert it into int or float type. int(print(‘ ‘))

Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.