简体   繁体   English

如果输入数字0,如何结束while循环并遵循其他说明

[英]How to end a while loop and follow the other instructions if the number 0 is entered

My code puts all of he users entered items into aa list I want it to stop when 0 is entered and follow the other commans such as sorting the list, deleting the highest and lowest values and then finding the average of them Here is the Code so far: 我的代码将所有用户输入的项目都放入一个列表中,我希望它在输入0时停止,并遵循其他命令,例如对列表进行排序,删除最高和最低值,然后查找它们的平均值。这就是代码,远:

i = 0
sizes = []
while i == 0:
    size = int(input("Enter the weight of your parcel in grams, enter 0 when done: "))
    sizes.append(size)
    if size < 1:
        break
sortedsizes = sorted(sizes)
largest = max(sizes)
smallest = min(sizes)
sizes.remove(largest)
sizes.remove(smallest)
print(sizes)

You do not need i and you do not want packages that weight less the 0. You might want to consider using float instead of int - where I live we measure kg for packages or g for letters - both will be taken including fractions ( 1.235kg or 5.28g). 你不需要i ,你不想要的软件包,体重少0。你可能要考虑使用float ,而不是int -我住的地方,我们衡量kg的包裹或g信件-无论将采取包括部分(1.235千克或5.28克)。

If someone inputs "22kg" any number conversion will crash - you should guard against it: 如果有人输入"22kg"任何数字转换都会崩溃-您应注意以下"22kg"

sizes = []
while True:
    try:
        size = int(input("Enter the weight of your parcel in grams, enter 0 when done: "))
        if size > 0:  
            sizes.append(size)
        elif size == 0:
            break
        else:
            raise ValueError()  # negative weight
    except ValueError:
        print("Only positive numbers or 0 to quit. Do not input text or kg/g/mg.")

sortedsizes = sorted(sizes) # you do nothing with this - why sort at all?
largest = max(sizes)
smallest = min(sizes)
sizes.remove(largest)
sizes.remove(smallest)
print(sizes)  # this prints the unsorted list that got min/max removed...

Output: 输出:

Enter the weight of your parcel in grams, enter 0 when done: 4 
Enter the weight of your parcel in grams, enter 0 when done: 3 
Enter the weight of your parcel in grams, enter 0 when done: 5 
Enter the weight of your parcel in grams, enter 0 when done: 6 
Enter the weight of your parcel in grams, enter 0 when done: -1 
Only positive numbers or 0 to quit. Do not input text or kg/g/mg.
Enter the weight of your parcel in grams, enter 0 when done: DONE 
Only positive numbers or 0 to quit. Do not input text or kg/g/mg. 
Enter the weight of your parcel in grams, enter 0 when done: 2 
Enter the weight of your parcel in grams, enter 0 when done: 0

[4, 3, 5]  # this is the unsorted list that got min/max removed...

If you want to remove only 1 maximal and 1 minimal value from the sorted list you can simplyfy it: 如果您只想从排序列表中删除1个最大值和1个最小值,则可以简单地将其删除:

sortedsizes = sorted(sizes)  
maxval = sortedsizes.pop()    # remove last one from sortedsizes  (== max)
minval = sortedsizes.pop(0)   # remove first one from sortedsizes (== min)
print(sortedsizes)            # print the sorted values

Doku: Doku:

if you want your code to stop(appending sizes to your list and continue with the commands after the repetitive procedure ) when 0 is entered, you have to do the opposite action. 如果要在输入0时停止代码(将大小附加到列表中并在重复过程之后继续执行命令),则必须执行相反的操作。

while True:
    size = int(input("Enter the weight of your parcel in grams, enter 0 when done: "))
    sizes.append(size)
    if size < 1:
        break

While the statement is false, the code continues from the sortedsizes = sorted(sizes) command. 当语句为false时,代码从sortedsizes = sorted(sizes)命令继续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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