Wondering why this while loop isn't stopping

Viewed 104
end_number = -99
num = (input("Enter Nnumber: "))
list_of_numbers = []
list_of_numbers.append(num)
while num != end_number:
    num = (input("Enter Nnumber: "))
print("The smallest number was", min(list_of_numbers))
print("The smallest number was", max(list_of_numbers))

I am trying to a have a person enter a bunch of int and stop when they ender -99. Then I'm wanting to print the smallest and largest in they entered.

6 Answers

input() returns a string and end_number is int, either convert the result of input() to int, with

num = int(input("Enter Nnumber: "))

or convert end_number to string, with

end_number = "-99"  # Double quotes to represent -99 as a String of characters
end_number = -99
list_of_numbers = []

while True:
    num = int(input("Enter Nnumber: "))

    if num == end_number:
        break
    else:
        list_of_numbers.append(num)

print("The smallest number was", min(list_of_numbers))
print("The smallest number was", max(list_of_numbers))

Some recaps:

  • Consider using while True
  • list_of_numbers.append(num) should be within while loop.
  • Your input should be int and within while loop.
  • You should checked whether num is equal to end_number within while loop.
  • You need break to stop the loop if num equals end_number

Your loop doesn't stop because num is not an integer when compared to end_number which is int in the beginning. Then it keeps repeating for input because input in while is also a string.

The element in list_of_numbers is a string and it only contains 1 element because it gets appended just once. So you should move your list_of_numbers.append(num) within while loop and convert num to int to get min and max value later.

You might need if and break to stop the loop when num equals end_numbers, otherwise it always gets -99 as min value.

So it goes like this:

while num != end_number:
    num = int(input("Enter Nnumber: "))
    if num == end_number: break
    list_of_numbers.append(num)

I am trying this

list_of_numbers = []
while (num := int(input("Enter Nnumber: "))) != -99: list_of_numbers.append(num)
print("The smallest number was", min(list_of_numbers))
print("The smallest number was", max(list_of_numbers))
end_number = -99
num = (input("Enter Nnumber: "))
list_of_numbers = []
list_of_numbers.append(num)
while num != end_number:
    num = int(input("Enter Nnumber: "))
print("The smallest number was", min(list_of_numbers))
print("The smallest number was", max(list_of_numbers))

Data Types In Python

In python, We have Data Types such as Integers, Strings, Floats and None Types.

  • Integers are whole numbers such as 5, 1, -234, 100134
  • Strings are words and sentences such as, "Hello, World!" or "Nice to meet you!"
  • Floats are decimal numbers, like, 0.342112, 4.98, -12.23

These data types can not be interchanged without the use of certain functions, For example.

"5" == 5 = False

5 == 5 = True

"5" == str(5) = True.

str() turns a data type into a string.

int() turns a data type into a integer

float() turns a data type into a decimal.

I hope this helps!

The main problem here is data types as input takes default type as string convert it to int to get value. as "-99"!=-99

end_number = -99
num = int(input("Enter Number: "))
list_of_numbers = []
list_of_numbers.append(num)
while num != end_number:
    num = int(input("Enter Number: "))
    list_of_numbers.append(num)
print("The smallest number was", min(list_of_numbers))
print("The smallest number was", max(list_of_numbers))
Related