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.
-
2The list_of_numbers.append(num) should be within the while loop. Otherwise how will the list get updated with the current inputs.Gautam Chettiar– Gautam Chettiar2022年09月19日 17:19:46 +00:00Commented 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.Code-Apprentice– Code-Apprentice2022年09月19日 20:52:24 +00:00Commented Sep 19, 2022 at 20:52
6 Answers 6
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
1 Comment
min(["1000", "99"]) returns "1000" due to string comparison.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
intand within while loop. - You should checked whether
numis equal toend_numberwithin while loop. - You need
breakto stop the loop ifnumequalsend_number
Comments
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)
Comments
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
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))
Comments
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!