[英]Wondering why this while loop isn't stopping
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.我试图让一个人输入一堆 int 并在他们 ender -99 时停止。 Then I'm wanting to print the smallest and largest in they entered.
然后我想打印他们输入的最小和最大的。
input()
returns a string and end_number
is int, either convert the result of input()
to int, with input()
返回一个字符串, end_number
为 int,或者将input()
的结果转换为 int,用
num = int(input("Enter Nnumber: "))
or convert end_number
to string, with或将
end_number
转换为字符串,使用
end_number = "-99" # Double quotes to represent -99 as a String of characters
Your loop doesn't stop because num
is not an integer when compared to end_number
which is int
in the beginning.您的循环不会停止,因为
num
与开头为int
的end_number
相比不是 integer 。 Then it keeps repeating for input because input in while
is also a string.然后它不断重复输入,因为
while
中的输入也是一个字符串。
The element in list_of_numbers
is a string and it only contains 1 element because it gets appended just once. list_of_numbers
中的元素是一个字符串,它只包含 1 个元素,因为它只被附加一次。 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.因此,您应该在
while
循环中移动list_of_numbers.append(num)
并将num
转换为int
以便稍后获取最小值和最大值。
You might need if
and break
to stop the loop when num
equals end_numbers
, otherwise it always gets -99 as min value.当
num
等于end_numbers
时,您可能需要if
和break
来停止循环,否则它总是将 -99 作为最小值。
So it goes like this:所以它是这样的:
while num != end_number:
num = int(input("Enter Nnumber: "))
if num == end_number: break
list_of_numbers.append(num)
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))
In python, We have Data Types such as Integers, Strings, Floats and None Types.在 python 中,我们有整数、字符串、浮点数和无类型等数据类型。
These data types can not be interchanged without the use of certain functions, For example.这些数据类型不能在不使用某些函数的情况下互换,例如。
"5" == 5 = False “5” == 5 = 假
5 == 5 = True 5 == 5 = 真
"5" == str(5) = True. “5” == str(5) = 真。
str() turns a data type into a string. str() 将数据类型转换为字符串。
int() turns a data type into a integer int() 将数据类型转换为 integer
float() turn a data type into a integer. float() 将数据类型转换为 integer。
I hope this helps!我希望这有帮助!
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:一些回顾:
while True
while True
list_of_numbers.append(num)
should be within while loop. list_of_numbers.append(num)
应该在 while 循环内。int
and within while loop.int
并且在 while 循环内。num
is equal to end_number
within while loop.num
是否等于end_number
。break
to stop the loop if num
equals end_number
num
等于end_number
,则需要break
来停止循环The main problem here is data types as input takes default type as string convert it to int to get value.这里的主要问题是数据类型,因为输入采用默认类型作为字符串将其转换为 int 以获取值。 as "-99"!=-99
作为“-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))
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.