Home > OS >  Why do I need to write number=int(input()) in this while loop again, when its already declared at th
Why do I need to write number=int(input()) in this while loop again, when its already declared at th

Time:10-28

I have some python code below to find the maximum and minimum values from a given input, but I'm not sure why I need to include number=int(input()) again within the while loop. If I remove this line of code in the while loop, the program enters an infinite loop and I'm not sure why.

number = int(input())

min_value = number

max_value = number

while (number>0):
    
    if (number>max_value):
        max_value = number
    if (number<min_value):
        min_value = number
    number=int(input())
print(min_value, 'and', max_value)

CodePudding user response:

Try this :

user_input = input("Enter the numbers separated by a space ").split()

int_list = list(map(int, user_input))

print ("Max number:",max(int_list), "\nMin number:",min(int_list))`

CodePudding user response:

The code will ask for input multiple times as it does not make sense to find maximum and minimum if you have only one number. Also, it will help you to exit the while loop whenever a 0 is entered.

  • Related