我是Python的初学者,并且遇到以下测试问题。编写一个Python程序,该程序定义一个名为myDate
的函数。 Tprogram必须提示用户输入该函数,即三个整数(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
我想获得有关如何获取整个消息的帮助,而不是仅一个月。
答案 0 :(得分:0)
我将像这样更改代码:
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)