结果是,当您输入任意数字1-7时,您将获得该数字的日期。出于某种原因,它总会在周一提示。我怎样才能解决这个问题?
'#Enter a number range 1-7 for the day of the week Example 1=Monday
#variables to represent the days of the week
num = float(input ("Enter the number for the day of week"))
Monday_number = 1
Tuesday_number = 2
Wednesday_number = 3
Thursday_number = 4
Friday_number = 5
Saturday_number = 6
Sunday_number = 7
Other_number = 8
#the day of the week
if Monday_number == 1:
print('Monday')
elif Tuesday_number == 2:
print('Tuesday')
elif Wednesday_number == 3:
print('Wednesday')
elif Thursday_number == 4:
print('Thursday')
elif Friday_number == 5:
print('Friday')
elif Saturday_number == 6:
print('Saturday')
elif Sunday_number == 7:
print('Sunday')
else:
if Other_number > 7:
print('Invalid number entered')'
答案 0 :(得分:1)
您不是将用户输入num
与任何内容进行比较。在if语句中,您应该按顺序将num
与每周的常量进行比较。更好的是,您可以使用查找表:
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
user_input = input('Enter the day of the week: ')
print(days[int(user_input)])
答案 1 :(得分:1)
逐步通过算法逐步完成算法。当你到达第一个if
声明时会发生什么?