'Can someone help me debug this? [closed]
Can someone please help me debug this? I can't figure out how to get the suffix to print. I also feel like there is a bunch of extra code in here that is not needed. Thank you!
import time
ageValid = True
def GetSuffix(age):
testString = '74'
testList = []
numberSuffix = {0:'th', 1:'st', 2:'nd', 3:'rd', 4:'th', 5:'th', 6:'th', 7:'th', 8:'th', 9:'th', 10:'th', 11:'th', \
12:'th', 13:'th', 14:'th', 15:'th', 16:'th', 17:'th', 18:'th', 19:'th'}
lastDigit = int(age) % 10
for key, value in numberSuffix.items():
if key == age:
print('Founder partner!')
print(value)
x = str(age)
return x+value
#stringNum = join(str(qualNum, key))
while True:
try:
age = input('Enter your age: ').strip()
GetSuffix(age)
except ValueError:
continue
else:
break
i = 0
while i < 1:
print(f'Happy {age}, Birthday!')
i += 1
# for key, value in knights.items():
print("{}: {}".format(key, value).title())
Solution 1:[1]
Here is an improved version of Sembei Norimaki's answer to correctly return th for 11th, 12th and 13th, also for 111th, 112th, 113th and the hundreds following!
def GetAgeWithSuffix(age):
# we will only put the digits that don't use "th" in the dict
# and use "th" as default if the key is not found
numberSuffix = {1:'st', 2:'nd', 3:'rd'}
lastDigit = int(age) % 10
if int(age%100) < 10 or int(age%100) > 20:
# if digit not found, return "th"
suffix = numberSuffix.get(lastDigit , 'th')
else:
suffix = 'th'
return str(age) + suffix
try:
age = int(input('Enter your age: '))
print(f"Happy {GetAgeWithSuffix(age)} Birthday!")
except ValueError:
print("Error, age must be an int")
Solution 2:[2]
import time
ageValid = True
def GetSuffix(age):
testString = '74'
testList = []
numberSuffix = {0:'th', 1:'st', 2:'nd', 3:'rd', 4:'th', 5:'th', 6:'th', 7:'th', 8:'th', 9:'th', 10:'th', 11:'th', \
12:'th', 13:'th', 14:'th', 15:'th', 16:'th', 17:'th', 18:'th', 19:'th'}
lastDigit = int(age) % 10
for key, value in numberSuffix.items():
# key is of type integer and age is of type string, you should typecast age to integer to compare
if key == int(age):
print('Founder partner!')
print(value)
x = str(age)
return x+value
#stringNum = join(str(qualNum, key))
while True:
try:
age = input('Enter your age: ').strip()
age = GetSuffix(age) # you should set the returned result to age to use it further
except ValueError:
continue
else:
break
i = 0
while i < 1:
print(f'Happy {age}, Birthday!')
i += 1
# for key, value in knights.items():
# print("{}: {}".format(key, value).title())
Solution 3:[3]
In your code you have to just assign the return of GetSuffix into age
age = GetSuffix(age)
If you want a clean version of your code with some improvements here's a simplified version:
def GetAgeWithSuffix(age):
# we will only put the digits that don't use "th" in the dict
# and use "th" as default if the key is not found
numberSuffix = {1:'st', 2:'nd', 3:'rd'}
lastDigit = age % 10
# if digit not found, use "th"
suffix = numberSuffix.get(lastDigit , 'th')
return str(age) + suffix
try:
age = int(input('Enter your age: '))
print(f"Happy {GetAgeWithSuffix(age)} Birthday!")
except ValueError:
print("Error, age must be an int")
Solution 4:[4]
You can omit using a dictionary in this problem, simple if/elif/else
statement will do the trick.
def get_suffix(number: int) -> str:
last_digit = number % 10
if last_digit == 1 and number != 11:
suffix = "st"
elif last_digit == 2 and number != 12:
suffix = "nd"
elif last_digit == 3 and number != 13:
suffix = "rd"
else:
suffix = "th"
return str(number) + suffix
while True:
try:
age = int(input('Enter your age: '))
if age <= 0:
print("Age must be positive.")
continue
except ValueError:
print("You entered not a number.")
continue
else:
break
suffixed_age = get_suffix(age)
print(f'Happy {suffixed_age}, Birthday!')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | iR0ckY |
Solution 3 | |
Solution 4 | Dmytro Kolupaiev |