抱歉我的英文。每个月我都去赌场玩,一个月一次。我的决定标准:用随机模块查找在赌场玩的日和周,只有一个例外,不接受随机模块上个月和周日播放。例如,如果我去了十月十四日,十一月的那一天应该是任何一天但不是十四日。这是我的代码:例如,如果我输入相同的数字“2”,程序不会消除我上个月的数字“2”。请帮忙。
import random
zz = raw_input ("Please enter the last week number: ")
fx = int(zz)
print fx
week_randoms=[]
gg = week_randoms
for i in range (4):
gg = week_randoms
gg.append(random.randrange(1,5))
print gg
hh = gg[0:1]
if fx != hh:
print "You got your number baby", hh
else:
print "Run this program again"
答案 0 :(得分:0)
试试这个,它将为您提供未来12个月的日期。 它将排除第29-31天,所以我们没有任何想象的日子,比如2月31日。
import random
days = range(1,29)
months = ['January','February','March','April','May','June','July','August','September','October','November','December']
for a in months:
b = random.choice(days)
days.remove(b)
print "Go on Day " + str(b) + " of the month of " + a " "."
答案 1 :(得分:0)
您可以跟踪所选的最后一天的数字,并确保不再选择它。今年(或任何其他闰年):
import random
months = ['January','February','March','April','May','June','July','August','September','October','November','December']
days_in_months = [31,29,31,30,31,30,31,31,30,31,30,31]
last_day_number = None
for month,ndays in zip(months,days_in_months):
while True:
day_number = random.randint(1,ndays)
if day_number != last_day_number:
break
last_day_number = day_number
print(day_number, month, '2016')
e.g。
28 January 2016
22 February 2016
21 March 2016
13 April 2016
21 May 2016
24 June 2016
25 July 2016
9 August 2016
16 September 2016
12 October 2016
5 November 2016
7 December 2016
祝你好运!
答案 2 :(得分:0)
xnx实施的略微修改版本:
import calendar
import random
last_pick = None
for i in range(1, 13):
month = calendar.month_name[i]
_, end = calendar.monthrange(2016, i)
while True:
day = random.randint(1, end)
if day != last_pick:
break
last_pick = day
print(day, month, 2016)