我正在尝试生成一个随机数,然后将该值分配给列表中的某一天。
我想要发生的是,如果选择的值是“0”,我想打印一次SUNDAY(0 + 1次)。如果它的“1”我想要它打印SUNDAY(然后是下一行)MONDAYMONDAY(1 + 1次),依此类推。请参阅此图片以获取完整示例。
更新
此代码已经尝试过:
import random
Random = random.randint(0, 6)
dayList = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
for dayList in Random(0):
print "Sunday" * 2
答案 0 :(得分:0)
这是满足要求的代码:
import random
dayList = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
def day_of_week(value):
return dayList[value] * (value + 1)
def print_script(value):
print('The number is', value)
for v in range(value + 1):
print(day_of_week(v))
value = random.randint(0, 6)
print_script(value)
只是为了测试所有可能性:
for v in range(7):
print_script(v)
输出:
The number is 0
Sunday
The number is 1
Sunday
MondayMonday
The number is 2
Sunday
MondayMonday
TuesdayTuesdayTuesday
The number is 3
Sunday
MondayMonday
TuesdayTuesdayTuesday
WednesdayWednesdayWednesdayWednesday
The number is 4
Sunday
MondayMonday
TuesdayTuesdayTuesday
WednesdayWednesdayWednesdayWednesday
ThursdayThursdayThursdayThursdayThursday
The number is 5
Sunday
MondayMonday
TuesdayTuesdayTuesday
WednesdayWednesdayWednesdayWednesday
ThursdayThursdayThursdayThursdayThursday
FridayFridayFridayFridayFridayFriday
The number is 6
Sunday
MondayMonday
TuesdayTuesdayTuesday
WednesdayWednesdayWednesdayWednesday
ThursdayThursdayThursdayThursdayThursday
FridayFridayFridayFridayFridayFriday
SaturdaySaturdaySaturdaySaturdaySaturdaySaturdaySaturday