使用字典和函数的Python代码

使用字典和函数的Python代码,python,python-2.7,Python,Python 2.7,我正在尝试使用字典和函数编写一个简单的python代码,如下所示 #Write a program to input number of week's day (1-7) and translate to its equivalent name of the day of the week. options = { "1" : "sunday", "2" : "monday", "3" : "tuesday", "4" :

我正在尝试使用字典和函数编写一个简单的python代码,如下所示

#Write a program to input number of week's day (1-7) and translate to its equivalent name of the day of the week.

options = { "1" : "sunday",
            "2" : "monday",
            "3" : "tuesday",
            "4" : "wednesday",
            "5" : "thursday",
            "6" : "friday",
            "7" : "saturday" }

options[raw_input("Enter number of weeks day (1-7):")]

def sunday():
    print "The day is Sunday"

def monday():
    print "The day is Monday"

def tuesday():
    print "The day is Tuesday"

def wednesday():
    print "The day is Wednesday"

def thursday():
    print "The day is Thursday"

def friday():
    print "The day is Friday"

def saturday():
    print "The day is Saturday"
输出是

[anishjp@localhost Python_programs]$ python hello24.py
Enter number of weeks day (1-7):1
[anishjp@localhost Python_programs]$

它不打印日期是星期天。谁能告诉我,我做错了什么?

字典选项的键是字符串。您可以改为存储函数:

但是,由于要引用这些函数,因此必须在字典定义之前声明它们:

def sunday():
    print "The day is Sunday"

...

def saturday():
    print "The day is Saturday"

options = { "1" : sunday,
            "2" : monday,
            "3" : tuesday,
            "4" : wednesday,
            "5" : thursday,
            "6" : friday,
            "7" : saturday}
最后,要调用它,必须传递参数。函数不需要任何参数,因此括号将为空:


字典选项的键是字符串。您可以改为存储函数:

但是,由于要引用这些函数,因此必须在字典定义之前声明它们:

def sunday():
    print "The day is Sunday"

...

def saturday():
    print "The day is Saturday"

options = { "1" : sunday,
            "2" : monday,
            "3" : tuesday,
            "4" : wednesday,
            "5" : thursday,
            "6" : friday,
            "7" : saturday}
最后,要调用它,必须传递参数。函数不需要任何参数,因此括号将为空:


实现这一点的简单方法是编写一个函数,该函数接受一个参数,即编号的日期,然后使用字典打印日期名称

#Write a program to input number of week's day (1-7) and translate to its equivalent name of the day of the week.

options = {
    "1": "sunday",
    "2": "monday",
    "3": "tuesday",
    "4": "wednesday",
    "5": "thursday",
    "6": "friday",
    "7": "saturday"
}

def print_day(day_number):
    day_name = options[day_number]
    print("The day is {}".format(day_name))

input_number = input("Enter number of weeks day (1-7):")
print_day(input_number)

显然,如果用户给出的无效数字小于1、大于7、不是整数等,您也可以在此处添加一些错误处理。

简单的方法是编写一个函数,该函数包含一个参数,即编号的日期,然后使用字典打印日期名称

#Write a program to input number of week's day (1-7) and translate to its equivalent name of the day of the week.

options = {
    "1": "sunday",
    "2": "monday",
    "3": "tuesday",
    "4": "wednesday",
    "5": "thursday",
    "6": "friday",
    "7": "saturday"
}

def print_day(day_number):
    day_name = options[day_number]
    print("The day is {}".format(day_name))

input_number = input("Enter number of weeks day (1-7):")
print_day(input_number)

显然,如果用户给出的无效数字小于1、大于7、不是整数等,您也可以在此处添加一些错误处理。

感谢您告知我代码可能采用的不同方法!谢谢你告诉我可能的不同方法,我的代码!