-2

I have been developing a small program. It works perfectly how it is but I want to make the code a bit smaller.

import time, math 
name= input("Enter Your Name: ")
age= int(input("Enter Your Age: "))
end= "th"
if age == 3 or age == 13 or age == 23 or age == 33 or age == 43 or age == 53 or age == 63 or age == 73 or age == 83 or age == 93:
 end= "rd"

if age == 2 or age == 22 or age == 32 or age == 42 or age == 52 or age == 62     or age == 72 or age == 82 or age == 92:
 end= "nd"

print ("Your Name Is "+ name + ", You Are " + str(age) + " Years Old.")
print ("Hi " + name + ", Happy " + str(age) + end + " birthday!")
time.sleep(5)

I would like to have an easier way to change the 'end' to other values without having to write them all, can I have it start at 3 then do it for everything 10 more than three.

demongolem
  • 9,474
  • 36
  • 90
  • 105
Bilbo
  • 56
  • 1
  • 9

6 Answers6

3

Use the modulo operator:

if age % 10 == 3:
    end = "rd"
elif age % 10 == 2:
    end = "nd"

Or use a dict:

ends = {2: "nd", 3: "rd"}
end = ends[age % 10]

You can also use a default:

ends = {1: "st", 2: "nd", 3: "rd"}
end = ends.get(age % 10, "th)
2

Extract the digit at tenth's place. Then it's straightforward. Though this question belongs to the codereview counterpart of SO.

import time, math
name= input("Enter Your Name: ")
age= int(input("Enter Your Age: "))

tenth_place = age % 10
if tenth_place == 3:
    end = "rd"
elif tenth_place == 2:
    end = "nd"
else:
    end = "th"

print ("Your Name Is "+ name + ", You Are " + str(age) + " Years Old.")
print ("Hi " + name + ", Happy " + str(age) + end + " birthday!")
time.sleep(5)
Shivendra
  • 1,076
  • 2
  • 12
  • 26
0
if age in range(3,93,10) :
    end = "rd"
Humbalan
  • 677
  • 3
  • 14
0

You can try this:

if int(age[-1]) == 3:
   end= "rd"

if int(age[-1]) == 2:
   end= "nd"
Stijn Van Daele
  • 285
  • 1
  • 14
0

Might not be shorter necessarily, but it works (and keeps 12th and 13th proper).

import time, math 

name = input("Enter Your Name:")
age = int(input("Enter Your Age:"))
end = "th"

# initializing age lists
list1 = []
list2 = []  

# filling list1 with ages 3-93 
for i in range(0,10):
    list1.append(10*i+3)

# filling list2 with ages 2-92
 for i in range(0,10):
    list2.append(10*i+2)

# if block to include correct suffix
for ages in list1:
    if ages == 13:
        end = end;
    elif ages == age:
        end = "rd"

for ages in list2:
    if ages == 12:
        end = end
    elif ages == age:
        end = "nd"

print ("Your Name Is "+ name + ", You Are " + str(age) + " Years Old.")
print ("Hi " + name + ", Happy " + str(age) + end + " birthday!")
time.sleep(5)
0

Thanks For All Of It Guys,

I have also found another flaw and fixed it, here is my current code. Thanks.

import time, math 
name= input("Enter Your Name: ")
age= int(input("Enter Your Age: "))
end= "th"



if age % 10 == 3:
     end = "rd"

elif age % 10 == 2:
     end = "nd"

elif age % 10 == 1:
     end = "st"

if age < 20 and age > 10:
 end = "th"



print ("Your Name Is "+ name + ", You Are " + str(age) + " Years Old.")
print ("Hi " + name + ", Happy " + str(age) + end + " birthday!")
time.sleep(2)

Thanks, Bilbo

Bilbo
  • 56
  • 1
  • 9