How to trigger this while loop to stop (Python)

Question:

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)
Asked By: Nolan

||

Answers:

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"):. 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. Just while True: is needed for this case.

There was also no need to use try: for the second while loop for taking user input for 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. 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. This is overall very bad code.

Answered By: cap1hunna
partAa= all(ele.isdigit() for ele in partA)

is partAa always False. There partA not iterable it’s string.

Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.