简体   繁体   English

想知道为什么这个while循环没有停止

[英]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与开头为intend_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时,您可能需要ifbreak来停止循环,否则它总是将 -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))

Data Types In Python Python 中的数据类型

In python, We have Data Types such as Integers, Strings, Floats and None Types.在 python 中,我们有整数、字符串、浮点数和无类型等数据类型。

  • Integers are whole numbers such as 5, 1, -234, 100134整数是整数,例如 5、1、-234、100134
  • Strings are words and sentences such as, "Hello, World!"字符串是单词和句子,例如“Hello, World!” or "Nice to meet you!"或“很高兴认识你!”
  • Floats are decimal numbers, like, 0.342112, 4.98, -12.23浮点数是十进制数,例如 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 = 假

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:一些回顾:

  • Consider using while True考虑使用while True
  • list_of_numbers.append(num) should be within while loop. list_of_numbers.append(num)应该在 while 循环内。
  • Your input should be int and within while loop.您的输入应该是int并且在 while 循环内。
  • You should checked whether num is equal to end_number within while loop.您应该在 while 循环中检查num是否等于end_number
  • You need 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM