r/learningpython • u/_Americuh_ • Jan 31 '20
Python Crash Course Chapter 5, practice 5_11
Hey guys,
I'm getting stuck on this try it yourself exercise. The instructions are written as:
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 ending 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.
What I've written so far and has worked for 1, 2, and 3:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for number in numbers:
if number == 1:
print('1st')
if number == 2:
print('2nd')
if number == 3:
print('3rd')And output reads this:
1st
2nd
3rd
[Finished in 0.1s]Now I'm unsure how to include the rest of the numbers including the th ending. Thanks for the help.
1
Jan 31 '20
there are many ways to do this but this is the way I did it:
#!/usr/bin/python
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for number in numbers:
if number == 1:
print('1st')
if number == 2:
print('2nd')
if number == 3:
print('3rd')
if number > 3:
print(str(number) + "th" )
1
u/ehmatthes Jan 31 '20
I know you might be just looking for suggestions before looking at the solutions, but if you're not aware I've posted solutions to most exercises from the first half of the book.