简体   繁体   English

写一个 function 给定日期编号,并返回日期名称(字符串)

[英]Write a function which is given the day number, and it returns the day name (a string)

Assume the days of the week are numbered 0,1,2,3,4,5,6 from Sunday to Saturday.假设从星期日到星期六,一周中的日子编号为 0、1、2、3、4、5、6。 Write a function which is given the day number, and it returns the day name (a string).写一个 function 给定日期编号,并返回日期名称(字符串)。 Help me solve this tusk pls.请帮我解决这个象牙。 And explain me pls why my code return me mistake: ValueError: invalid literal for int() with base 10: ''请解释一下为什么我的代码返回错误:ValueError: invalid literal for int() with base 10: ''

d1 = "Понедельник"
d2 = "Вторник"
d3 = "Среда"
d4 = "Четверг"
d5 = "Пятница"
d6 = "Суббота"
d7 = "Воскресенье"

print (input ("Chose day of number: "))
x = int(input())

if x == 1:

       print (d1)

print("Program ended")

You've used the input function in the wrong way, please try something like this:您以错误的方式使用了input function,请尝试以下操作:

d1 = "Понедельник"
d2 = "Вторник"
d3 = "Среда"
d4 = "Четверг"
d5 = "Пятница"
d6 = "Суббота"
d7 = "Воскресенье"

x = int(input("Chose day of number: "))

if x == 1:
    print(d1)

print("Program ended")

I think you're a beginner.我认为你是一个初学者。 You can use dict , list etc. to make your program simpler.您可以使用dictlist等来使您的程序更简单。

A dictionary can be used for solving this requirement easily.可以使用字典轻松解决此要求。

Here is the working solution that uses the Python dictionary:这是使用 Python 字典的工作解决方案:

# File name: Weekdays.py

weekDays = {
    1: "Понедельник",
    2: "Вторник",
    3: "Среда",
    4: "Четверг",
    5: "Пятница",
    6: "Суббота",
    7: "Воскресенье"
}


userInput = input("Choose a number for day (1 to 7): ")
if(userInput.isdigit() and (int(userInput) > 0) and (int(userInput) < 8)):
    n = int(userInput)
    print(weekDays[n])
else:
    print("Input must be a number between 1 and 7 only")

Output: Output:

> python Weekdays.py
Choose a number for day (1 to 7): 6
Суббота

> python Weekdays.py
Choose a number for day (1 to 7): 1
Понедельник

> python Weekdays.py
Choose a number for day (1 to 7): 99
Input must be a number between 1 and 7 only

> python Weekdays.py
Choose a number for day (1 to 7): 0
Input must be a number between 1 and 7 only

> python Weekdays.py
Choose a number for day (1 to 7): 7
Воскресенье

try this one:试试这个:

def days_func(day_index):
    days = {0: 'Monday', 1: 'Tuesday', 2: 'Wednesday', 3: 'Thursday', 4: 'Friday', 5: 'Saturday', 6: 'Sunday'}

    if day_index in range(0, 6):
        return days[day_index]
    else:
        message = 'Invalid Number, try again!'
        return message


try:
    day = int(input('Enter the number of a day : '))
    print(days_func(day))
except ValueError:
    print('Index must be Integer!')

Error because of other character than digit as input.Try to use dictionary to store data and fetch it as requirement.错误,因为输入的字符不是数字。尝试使用字典来存储数据并根据需要获取它。 https://www.programiz.com/python-programming/dictionary https://www.programiz.com/python-programming/dictionary

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写一个程序来打印与用户给出的日期编号相对应的日期名称 - How to write a program to print name of day corresponding to the day number given by user 计算给定日期的记录数 - Count number of records for a given day 创建一个函数来计算给定日期和城市播放的歌曲数量 - Create a function to calculate the number of songs played for a given day and city 我需要编写一个 function 来打印给定月份的一周,其中 arguments 周数,开始日期和月份中的天数(Python) - I need to write a function to print out a week of a given month, with the arguments week number, start day and days in the month (Python) 以特定格式从给定日期获取一天的名称 - Get name of a day from given day in specific format 给定的字符串数组包含 X 个单词,返回给定日期的相同单词 - Given array of string contain X number of words return the same word for a given day 给定日期以熊猫为单位的四分之一的天数 - day number of a quarter for a given date in pandas 根据给定日期获取日期名称 - Get day name based on a given date Python:使用日历将日期数字转换为日期名称 - Python: Converting a day number to a day name using calendar Python 从数字打印星期的名称 - Python print day name of week from number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM