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()
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:')
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')
- You need to use input instead of the print function.
- 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')
Print() function returns str type value, you need to convert it into int or float type. int(print(‘ ‘))
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()
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:')
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')
- You need to use input instead of the print function.
- 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')
Print() function returns str type value, you need to convert it into int or float type. int(print(‘ ‘))