4
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.

asked Sep 19, 2022 at 17:17
2
  • 2
    The list_of_numbers.append(num) should be within the while loop. Otherwise how will the list get updated with the current inputs. Commented Sep 19, 2022 at 17:19
  • Read this article for tips on how to debug your code. These techniques can help you get closer to solving problems like this on your own. Commented Sep 19, 2022 at 20:52

6 Answers 6

7

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
thefourtheye
241k53 gold badges466 silver badges505 bronze badges
answered Sep 19, 2022 at 17:19
Sign up to request clarification or add additional context in comments.

1 Comment

Converting the inputs to int is necessary to get the expected output. Otherwise, as an example, min(["1000", "99"]) returns "1000" due to string comparison.
0
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
answered Sep 19, 2022 at 19:52

Comments

0

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)
answered Sep 19, 2022 at 20:48

Comments

0

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))

Comments

-1

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))
answered Sep 19, 2022 at 19:18

Comments

-1
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!

answered Sep 19, 2022 at 18:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.