mercredi 21 février 2018

Looping through a list in Python

Trying to loop through a list of numbers so that the output reads the result on a seperate line.

Instructions Given: - Store numbers 1-9 in a list. - Loop through the list. - Using if-elif-else chain inside the loop to print the appropriate ending for each number. The output should read "1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th" with each result on a seperate line. Most ordinal numbers end in "th" except for 1st, 2nd, 3rd.

My Problem: I am having an issue with the individual loop code.

What is the correct way to write this?

Thank you for the help in advance!

numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9']

for numbers in numbers:
        if '1' in numbers:
                print(" + number +" "st.")
        elif '2' in numbers:
                print(" + number + " "nd.")
        elif '3' in numbers:
                print(" + number + " "rd.")
        else:
                print(" + number +" "th.")

Aucun commentaire:

Enregistrer un commentaire