Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to Stop Sentinel Value from Entering Final Output
#1
I am writing a program using while loop to determine the highest and lowest of a user's inputted numbers, allowing the user to enter as many numbers as they wish, before using the sentinel value of -1 to exit. Then, the program should print the lowest and the highest inputted values. My problem is, the sentinel value keeps entering the loop as an input value and is being considered part of the range. If the user does not enter a number below my sentinel value of -1, then the sentinel value is what appears as the lowest. I've spent time trying to fix it but without success. Does anyone have any ideas as to why this is happening and what should be done to mend it? Here is my code:

minimum = int()
highest = int()
while(True):
    num = int(input("Enter a Number (Use -1 to Stop): " ))  
    if highest < num:
        highest = num
    if minimum > num:
        minimum = num  
    if(num == -1):
        break      
   
print("The interval is",minimum,"-", highest)
Quote
#2
Move lines 9 and 10 above line 5.
Quote
#3
If you enter not a number, it will raise a ValueError.
If you enter for example abc, int can't convert this to an integer.
Catch the ValueError
Ask the user to use q or Q to quit the program.


# usually it's not good to
# set a name to None
# and afterwards to an integer
minimum = None # not set yet
maximum = None # not set yet
# if you forget this, you'll run
# into a TypeError, happens for example with comparison < or >


while True: # <- no parenthesis needed
    user_input = input("Enter a Number (q|Q to quit): ")
    if user_input.lower() == 'q':
        break
    try:
        num = int(user_input)
    except ValueError:
        print(user_input, 'is not a valid integer')
        # ask again for user input
        continue
    if minimum is None:
        # the first time minimum and maximum
        # is None
        # Just setting minimum
        # and maximum to the first user input
        # this this will never again executed
        minimum = num
        maximum = num
        # the code afterwards will be executed
        # but does no change to the value
    if maximum < num:
        maximum = num
    if minimum > num:
        minimum = num
    # fancy format string: Python 3.6+
    # first minimum and maximum is equal
    print(f"The interval is {minimum} -> {maximum}")


print('Program ends here')
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote
#4
Another approach would be use list and built-in min(), max() functions. With Python 3.8 walrus operator one can write:

answers = []

while (answer := input('Enter a number (q|Q) to quit): ').lower()) != 'q':
    try:
        answers.append(int(answer))
    except ValueError:
        print(f'Expected integer but got {answer!r}')

if answers:
    print(f'Mininum value {min(answers)} and maximum value {max(answers)}')
else:
    print('No integers entered!')
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Error when entering letter/character instead of number/integer helplessnoobb 2 411 Jun-22-2019, 07:15 AM
Last Post: ThomasL
  Program not entering if statement on false. Scottx125 4 484 Nov-12-2018, 06:30 PM
Last Post: Scottx125
  Entering an expression with "input" command johnmnz 3 1,178 Sep-01-2017, 05:20 PM
Last Post: metulburr
  Trouble when entering the number sylas 24 5,844 Apr-02-2017, 01:09 PM
Last Post: sparkz_alot
  How can I make a sentinel value NOT be initialized in a class/method - OOP? netrate 2 1,308 Jan-14-2017, 07:34 AM
Last Post: stranac
  Final exam calculator iliketocode 1 1,640 Jan-08-2017, 07:06 PM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)