首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何提示用户输入函数的整数,并以字符串消息的形式返回?

如何提示用户输入函数的整数,并以字符串消息的形式返回?
EN

Stack Overflow用户
提问于 2020-10-05 16:15:18
回答 1查看 50关注 0票数 0

我是Python的初学者,我有以下测试题。编写一个Python程序,定义一个名为myDate的函数。程序必须提示用户输入函数,即三个整数(nDaynMonthnYear)。函数的输出必须是描述您生日的字符串消息。

例如:

代码语言:javascript
复制
You were born on 10 october 1995.

函数调用theMessage = myDate(nDay, nMonth, nYear)时,后面必须跟一个print(theMessage)

我试了一下,如下:

代码语言:javascript
复制
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

我想得到的帮助,如何获得整个消息,而不仅仅是一个月。

EN

回答 1

Stack Overflow用户

发布于 2020-10-05 16:57:31

我将像这样修改代码:

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64204818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档