如何解决如何提示用户输入函数的整数,并将其作为字符串消息返回?
我是Python的初学者,并且遇到以下测试问题。编写一个Python程序,该程序定义一个名为myDate
的函数。 Tprogram必须提示用户输入该函数,即三个整数(nDay
,nMonth
,nYear
)。该函数的输出必须是描述您生日的字符串消息。
例如:
You were born on 10 october 1995.
函数调用theMessage = myDate(nDay,nMonth,nYear)
后面必须带有print(theMessage)
。
我尝试如下:
def myDate(nDay,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,nYear)
print(theMessage)
output: October
解决方法
我将像这样更改代码:
import datetime
def myDate(nDay,nMonth,nYear):
theDate = datetime.date(nYear,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,nYear)
print(theMessage)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。