Home > Back-end >  Need help fixing Incorrect code to ask the user user if they would like to print the numbers in asce
Need help fixing Incorrect code to ask the user user if they would like to print the numbers in asce

Time:10-09

I just need someone to correct my code to do exactly the following instructions:

Create a Python program that prints the numbers from 1 to 10. The program will ask the user if they would like to print the numbers in ascending or descending order.

If printing the numbers in ascending (1 to 10) order, you must use a while loop in the printing of the numbers. If printing the numbers in descending (10 to 1) order, you must use a for (with the range function) loop in the printing of the numbers.

My Code:

input("Would you like to print the numbers in ascending or descending order?: ")
if input ("ascending"):
 print(num)
elif input ("descending"):
    print (range)

num = 1
while num < 11:
    print(num)
    num = num   1
    
for range in [10,9,8,7,6,5,4,3,2,1]:
    print(range)

CodePudding user response:

You need to save the return value of the input prompt:

answer = input("ascending or descending? ")
if answer == "ascending":
    ...
elif answer == "descending":
    ...

CodePudding user response:

Just move the while loop after checking if ascending & move the for loop bellow checking if descending. Though, your for loop could be written in better way. I am skipping that part.

if input ("ascending"):
    # move your while loop code here
elif input ("descending"):
    # move your for loop code here
  • Related