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.")
-
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...jonrsharpe– jonrsharpe2018年02月21日 18:00:32 +00:00Commented Feb 21, 2018 at 18:00
-
don't use if '1' in numbers use with if '1'==numbers that's it for allNarendra– Narendra2018年02月21日 18:02:22 +00:00Commented Feb 21, 2018 at 18:02
4 Answers 4
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.")
2 Comments
str.format() digitalocean.com/community/tutorials/… best of luck to you!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.
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
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 . ')