[英]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.您可以使用
dict
、 list
等来使您的程序更简单。
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.