Reputation: 21
I am a beginner in python. My simple three if statements below only seems to reach the first 'if' and strangely the last 'if'. And what's also strange is that when I type in a number higher than 9, the high number variable won't store a number past that. I've quadruple checked the indentations. What is happening?
tldr; just trying to create a simple python script to figure out high numbers from a series of inputs and low numbers from a series of inputs. I type done when finished inputting.
#!usr/bin/python
number = 0 #init number variable to zero
highNum = 0 #init the highest number to 0
lowNum = 99 #init the lowest number to 99
while number != 'done':
number = raw_input('Enter a Number: ')
if number > highNum:
highNum = number
if number < lowNum:
lowNum = number
if number == "done":
break
print "Low Number is : ", lowNum
print "High Number is: ", highNum
The output I get is:
Enter a Number: 16
lowNum : 99
highNum: 16
Enter a Number: 17
lowNum : 99
highNum: 17
Enter a Number: 9
lowNum : 99
highNum: 9
Enter a Number: 17
lowNum : 99
highNum: 9
Upvotes: 1
Views: 290
Reputation: 6633
You need to convert your input to an integer data type in order to compare it to other integers..
while True:
userTyped = raw_input('Enter a Number: ')
if userTyped == "done":
break
else:
number = int(userTyped)
if number > highNum:
highNum = number
if number < lowNum:
lowNum = number
print "Low Number is : ", lowNum
print "High Number is: ", highNum
Upvotes: 1