Python代码有问题。目的是在输入过程中对后缀进行编码

Python代码有问题。目的是在输入过程中对后缀进行编码,python,python-3.x,Python,Python 3.x,目标:编写一个函数,将整数作为其唯一参数,并返回该整数的序号缩写作为其唯一结果。例如,如果传递给函数的是整数1,那么它应该返回字符串“1st”。如果传递给它的是整数12,那么它应该返回字符串“12th”。如果通过2003,则应返回字符串“2003rd”。您的函数不能在屏幕上打印任何内容 def convert (n): self.num = num n = int(self.num) if 4 <= n <= 20: suffix = 'th

目标:编写一个函数,将整数作为其唯一参数,并返回该整数的序号缩写作为其唯一结果。例如,如果传递给函数的是整数1,那么它应该返回字符串“1st”。如果传递给它的是整数12,那么它应该返回字符串“12th”。如果通过2003,则应返回字符串“2003rd”。您的函数不能在屏幕上打印任何内容

def convert (n):
    self.num = num
    n = int(self.num)
    if 4 <= n <= 20:
         suffix = 'th'
    elif n == 1 or (n % 10) == 1:
         suffix = 'st'
    elif n == 2 or (n % 10) == 2:
         suffix = 'nd'
    elif n == 3 or (n % 10) == 3:
        suffix = 'rd'
    elif n < 100:
        suffix = 'th'
    ord_num = str(n) + suffix
    return ord_num

def main ():

    day = int(input("Enter the day:"))
    month = int(input("Enter the month:"))
    year = int(input("Enter the year:"))

    print("on the %n" %n, convert(day), "day of the %n" %month,
          convert(month), "month of the %n" %year, convert(year),",
          something amazing happened!")

    main()
def转换(n):
self.num=num
n=int(self.num)

如果4这可能更接近您想要的:

def convert(n):
    n = int(n)
    suffix = ''
    if 4 <= n <= 20:
         suffix = 'th'
    elif n == 1 or (n % 10) == 1:
         suffix = 'st'
    elif n == 2 or (n % 10) == 2:
         suffix = 'nd'
    elif n == 3 or (n % 10) == 3:
        suffix = 'rd'
    elif n < 100:
        suffix = 'th'
    return str(n) + suffix


def main ():
    day = int(input("Enter the day: "))
    month = int(input("Enter the month: "))
    year = int(input("Enter the year: "))

    print("on the %s day of the %s month of the %s, something amazing happened!" %
          (convert(day), convert(month), convert(year)))

main()
def转换(n):
n=int(n)
后缀=“”

如果4这可能更接近您想要的:

def convert(n):
    n = int(n)
    suffix = ''
    if 4 <= n <= 20:
         suffix = 'th'
    elif n == 1 or (n % 10) == 1:
         suffix = 'st'
    elif n == 2 or (n % 10) == 2:
         suffix = 'nd'
    elif n == 3 or (n % 10) == 3:
        suffix = 'rd'
    elif n < 100:
        suffix = 'th'
    return str(n) + suffix


def main ():
    day = int(input("Enter the day: "))
    month = int(input("Enter the month: "))
    year = int(input("Enter the year: "))

    print("on the %s day of the %s month of the %s, something amazing happened!" %
          (convert(day), convert(month), convert(year)))

main()
def转换(n):
n=int(n)
后缀=“”

如果现在这样做更有意义的话!我从来都不知道梅恩的事谢谢你,迈克!!很高兴听到这有帮助。你可以接受一个答案()来表示它解决了你的问题。现在这就更有意义了!我从来都不知道梅恩的事谢谢你,迈克!!很高兴听到这有帮助。您可以接受一个答案()来表示它解决了您的问题。