我是Python的初学者,我有以下测试题。编写一个Python程序,定义一个名为myDate的函数。程序必须提示用户输入函数,即三个整数(nDay、nMonth、nYear)。函数的输出必须是描述您生日的字符串消息。
例如:
You were born on 10 october 1995.函数调用theMessage = myDate(nDay, nMonth, nYear)时,后面必须跟一个print(theMessage)。
我试了一下,如下:
def myDate(nDay, nMonth, nYear):
... if nMonth == 1:
... return "January"
... if nMonth == 2:
... return "February"
... if nMonth == 3:
... return "March"
... if nMonth == 4:
... return "April"
... if nMonth == 5:
... return "May"
... if nMonth == 6:
... return "June"
... if nMonth == 7:
... return "July"
... if nMonth == 8:
... return "August"
... if nMonth == 9:
... return "September"
... if nMonth == 10:
... return "October"
... if nMonth == 11:
... return "November"
... if nMonth == 12:
... return "December"
...
nDay = int(input("Day"))
Day>? 25
nMonth = int(input("Enter a number between 1 and 12: "))
Enter a number between 1 and 12: >? 10
nYear = int(input("Year"))
Year>? 1998
theMessage = myDate(nDay, nMonth, nYear)
print(theMessage)
output: October我想得到的帮助,如何获得整个消息,而不仅仅是一个月。
发布于 2020-10-05 16:57:31
我将像这样修改代码:
import datetime
def myDate(nDay,nMonth,nYear):
theDate = datetime.date(nYear, nMonth, nDay).strftime('You were born on %d %B %Y')
return theDate
nDay = int(input("Day"))
nMonth = int(input("Enter a number between 1 and 12: "))
nYear = int(input("Year"))
theMessage = myDate(nDay, nMonth, nYear)
print(theMessage)https://stackoverflow.com/questions/64204818
复制相似问题