Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I just want to ask what would I use to loop my code? I want the operation to loop and will ask again if to continue or not..

What I have tried:

Python
list=input("Enter your list:")
print("Unsorted:", list.split())

a=list
print("Sorted Ascending Order:")
print sorted(a, key=None, reverse=False)

print("Sorted Descending Order:")
print sorted(a, key=None, reverse=True)
Posted
Updated 12-Apr-21 0:52am
v3

There are only two loop types in Python: for and while: Loops - Learn Python - Free Interactive Python Tutorial[^]
A for loop is used when you want to repeat some code a specific number of times.
A while loop is used when you want to repeat some code until something specific changes, or happens.

Since you don't know in advance how many times the user will want to answer "Yes, please continue" a for loop is not likely to be useful.
 
Share this answer
 
Try, for instance (Note: it is Python 3 code)
Python
wish_to_continue = 'Y'
while wish_to_continue == 'Y':
  list=input("Enter your list:")
  print("Unsorted:", list.split())

  a=list
  print("Sorted Ascending Order:")
  print(sorted(a, key=None, reverse=False))

  print("Sorted Descending Order:")
  print(sorted(a, key=None, reverse=True))

  wish_to_continue = input("Do you wish to continue (Y)?")
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900