1

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
Chad S.
  • 6,252
  • 15
  • 25
Montana
  • 21
  • 1
  • 2

1 Answers1

1

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
Chad S.
  • 6,252
  • 15
  • 25
  • Yup, This works. I think I need to get a better idea of what "while True:" is, since I don't get while what is true...in order words, what would make it false. Thank you. – Montana Nov 05 '15 at 16:40
  • Nothing will make it false, it will loop forever until the `userTyped == 'done'` – Chad S. Nov 05 '15 at 16:50