简体   繁体   English

如何触发此while循环停止(Python)

[英]How to trigger this while loop to stop (Python)

So, I have been trying to get this program to sort the numbers that are put in. I want the program to sort them based on a second parameter that the user sets.所以,我一直试图让这个程序对输入的数字进行排序。我希望程序根据用户设置的第二个参数对它们进行排序。 I have gotten that part to work, and am trying to get it so that it has to be one of the three outputs or loops.我已经让这部分工作,并试图让它成为三个输出或循环之一。 Even if one of the correct inputs is put in, it is still looping.即使输入了正确的输入之一,它仍然在循环。 Here is the code:这是代码:

while True:
    try:
        partA=input("Please insert a list of numbers separated by a space ")
        partAa= all(ele.isdigit() for ele in partA)
    except partAA==False:
        print("Error, please try again")
        continue
    except partA=="":
        print("Error, please input numbers ")
        continue
    else:
        break
partB=partA.split()
print(partB)

for i in range(len(partB)):
    partB[i]=int(partB[i])
order=""
while order!=("ascending") or order!=("descending") or order!=("none"):
    try:
        order=input("What order would you like your numbers in? ascending, descending, or none? \n")
    if order==("ascending") or order==("descending") or order==("none"):
        break
    else:
        print("Error, please try again")
        continue

if order=="ascending":
    partB.sort()
    print(partB)
elif order=="descending":
        partB.sort(reverse=True)
        print(partB)
elif order=="none":
        print(partB)
while True:
    try:
        partA=input("Please insert a list of numbers separated by a space ")
        partAa= all(ele.isdigit() for ele in partA)
        break
    except partAA==False:
        print("Error, please try again")
        continue
    except partA=="":
        print("Error, please input numbers ")
        continue

partB=partA.split()
print(partB)

for i in range(len(partB)):
    partB[i]=int(partB[i])
order=""
while True:
    order=input("What order would you like your numbers in? ascending, descending, or none? \n")
    if order==("ascending") or order==("descending") or order==("none"):
        break
    else:
        print("Error, please try again")
        continue

if order=="ascending":
    partB.sort()
    print(partB)
elif order=="descending":
    partB.sort(reverse=True)
    print(partB)
elif order=="none":
    print(partB)

You need to add breaks to the conditions you want the loop to end after.您需要在希望循环结束的条件中添加中断。 You want it to end after the user input, therefore you add your breaks after the user input.您希望它在用户输入后结束,因此您在用户输入后添加中断。 You're 'breaking' out of the loop.你正在“打破”循环。

Also, there was no need to loop with the conditions while order:=("ascending") or order!=("descending") or order!=("none"): .此外,没有必要循环条件while order:=("ascending") or order!=("descending") or order!=("none"): You break the loop once user has input the desired strings.一旦用户输入了所需的字符串,您就会中断循环。 There is no need to loop on the condition the user inputs one of those strings or not because your break ends the loop before the condition is even met.无需根据用户输入这些字符串之一的条件进行循环,因为您的break在条件满足之前就结束了循环。 Just while True: is needed for this case. Just while True:在这种情况下是需要的。

There was also no need to use try: for the second while loop for taking user input for order .对于第二个 while 循环,也不需要使用try:来获取order的用户输入。 You only use try: if an exception error could be raised and you need the program to make a decision based on that error if it happens.您只使用try: if 可能引发异常错误,并且如果发生错误,您需要程序根据该错误做出决定。 You also didn't even add an except: line with try: which would have raised an error had you even reached that part of the script.您甚至没有在try:中添加except:行,如果您到达脚本的那部分,它会引发错误。 This is overall very bad code.这是整体非常糟糕的代码。

partAa= all(ele.isdigit() for ele in partA)

is partAa always False.partAa总是 False。 There partA not iterable it's string.有部分不可迭代它是字符串。

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

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