-1

i wrote this piece of code that arranges the numbers given by the order that user applies but there is a problem with the last part where if user enter's somthing random it just infinite loop's the "else" how can i fix this? `

#Taking multiple inputs from user entries
x = list(input("Enter multiple value: ").split())
print("List of numbers: ", x)

user = str(input("do you want accending or decending order? "))

while True:
    if user == "accend":
        x.sort()
        print(x)
        break
    elif user == "decend":
        x.sort(reverse=True)
        print(x)
        break
    else:
        print("are you stupid? >:( try again! ")

print("congtaz! here are you'r numbers showing in "+ user +" order")

`

i just dont get the logic of while in this problem

Element
  • 3
  • 1
  • you have to move user inside the while loop – Sterling Dec 08 '22 at 23:13
  • `split()` returns a list, you don't need to call `list()`. – Barmar Dec 08 '22 at 23:13
  • The loop never updates `user`. So if it's not valid the first time, it will never become valid and the loop will repeat forever. – Barmar Dec 08 '22 at 23:14
  • Put the input function into your while loop, the user cant try again if you have a constant that just gets checked all over again. Also, the split() function already returns a list, so no need to use the list() function. – wallbloggerbeing Dec 08 '22 at 23:16

1 Answers1

0

The reason of the infinite loop is that once you enter anything besides "ascend" or "decend", it is stored in user, then when you enter the while, all it is doing is repeating while-else part and never let the variable user to change anymore. What you need is to use input() again in the else and thus let the user to change the variables content.

L D
  • 593
  • 1
  • 3
  • 16