0

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?

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.")
2
  • What issue? Give a minimal reproducible example. But using the same name for the loop variable as the thing it's looping over is probably a bad idea... Commented Feb 21, 2018 at 18:00
  • don't use if '1' in numbers use with if '1'==numbers that's it for all Commented Feb 21, 2018 at 18:02

4 Answers 4

1

When you say:

if '1' in numbers:

You are checking if that item is in the list, which will be true for every single iteration, so every iteration is going to print '1st'.

What you need to do is check the individual value, which you've specified as numbers but should change to number for each iteration

for number in numbers:
    if number == '1': print('{}st.'.format(number))

Hope that makes sense!

Also I just noticed, print(" + number +" "st.") I'm assuming you're attempting to concatenate strings here. I'd suggest using format, as I've shown above. However to concatenate this statement you'd want to say print(number + "st.")

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for taking the time to explain this to a rookie! This helped me solve my problem. I have not learned the format you suggested yet, but I know it now!
This is a great article on str.format() digitalocean.com/community/tutorials/… best of luck to you!
0

You are close. In your for loop, you want the enumerator to be different than the list you are enumerating. Then that enumerator contains the object you are comparing, so in the print you don't need the quotes around number

numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
for number in numbers:
    if '1' in number:
        print( number +"st.")
    elif '2' in number:
        print(number + "nd.")
    elif '3' in number:
        print( number + "rd.")
    else:
        print(number +"th.")

To answer your last question, there is no one correct way to do it. Different people have different approaches depending on personal preference and level of knowledge of the language.

2 Comments

This worked great. Thank you for taking the time to assist a rookie!
Cool. Take a look at Robbies answer too as it is preferable to use: if number == '1':
0
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]   

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

Comments

0

Ordinal Numbers: Ordinal numbers indicate their position in a list, such as 1st or 2nd. Most ordinal numbers end in th, except 1, 2, and 3. •Store the numbers 1 through 9 in a list. •Loop through the list. •Use an if- elif- else chain inside the loop to print the proper ordinal end- ing for each number. Your output should read "1st 2nd 3rd 4th 5th 6th 7th 8th 9th", and each result should be on a separate line.

numbers = []
for number in range(1,10):
    print(number )
    if number == 1:
        print('1st')
    elif number == 2:
        print('2nd')
    elif number == 3:
        print('3rd')
    elif number == 4:
        print('4th')
    elif number == 5:
        print('5th')
    elif number == 6:
        print('6th')
    elif number == 7:
        print('7th')
    elif number == 8:
        print('8th')
    elif number == 9:
        print('9th')
    else:
        print('\nFind the right way to solve this example . ')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.