import random
box=('白1','白2','白3','黑4','黑5','黑6')
x=random.randint(0,5)
print(box[x]) # 切片用法
为了更好地检查输出结果,调用一个循环验证
[Python] 纯文本查看复制代码
import random
box=('白1','白2','白3','黑4','黑5','黑6')
for i in range(30):
x=random.randint(0,5)
print(box[x])
将两次摸球的结果输出
[Python] 纯文本查看复制代码
import random
box=('白1','白2','白3','黑4','黑5','黑6')
for i in range(2):
x=random.randint(0,5)
print(box[x])
判断两次的结果(分别调用两个结果)
[Python] 纯文本查看复制代码
import random
box=('白1','白2','白3','黑4','黑5','黑6')
x=random.randint(0,5)
y=random.randint(0,5)
a=box[x]
b=box[y]
print(a,b)
if a[0]==b[0] and a[0]=="白" : # 还能 a[0]==b[0]=="白" 这样写?
print('赢了7元')
else:
print('输了3元')
多次进行游戏
[Python] 纯文本查看复制代码
import random
box=('白1','白2','白3','黑4','黑5','黑6')
for i in range(10):
x=random.randint(0,5)
y=random.randint(0,5)
a=box[x]
b=box[y]
print(a,b)
if a[0]==b[0] and a[0]=="白" :
print('赢了7元')
else:
print('输了3元')
是否继续游戏由玩家决定
[Python] 纯文本查看复制代码
import random
box=('白1','白2','白3','黑4','黑5','黑6')
for i in range(10):
c=input("是否继续(1继续、2退出):")
if c=="1" :
x=random.randint(0,5)
y=random.randint(0,5)
a=box[x]
b=box[y]
print(a,b)
if a[0]==b[0] and a[0]=="白" :
print('赢了7元')
else:
print('输了3元')
else:
break
记录总花费与盈利
[Python] 纯文本查看复制代码
import random
get=0
lost=3
box=('白1','白2','白3','黑4','黑5','黑6')
for i in range(10):
c=input("*****是否继续(1继续、2退出):*****")
if c=="1" :
x=random.randint(0,5)
y=random.randint(0,5)
a=box[x]
b=box[y]
print(a,b)
if a[0]==b[0] and a[0]=="白" :
print('赢了7元')
get=get+7
else:
print('输了3元')
i=i+1
print('总共花费了',lost*i,'赢了',get)
else:
break
简单的优化一下,让结果更突出,去除多余信息
[Python] 纯文本查看复制代码
import random
get=0
lost=3
box=('白1','白2','白3','黑4','黑5','黑6')
for i in range(10):
c=input("*****是否继续(1继续、2退出):*****")
if c=="1" :
x=random.randint(0,5)
y=random.randint(0,5)
a=box[x]
b=box[y]
print(a,b)
if a[0]==b[0] and a[0]=="白" :
print('赢了7元')
get=get+7
i=i+1
print('总共花费了',lost*i,'赢了',get)
else:
break
二、获奖的概率
尝试用大量真实数据计算出来
[Python] 纯文本查看复制代码
import random
win=0
lose=0
box=('白1','白2','白3','黑4','黑5','黑6')
z=int(input('请输入试验次数:'))
for i in range(z):
x=random.randint(0,5)
y=random.randint(0,5)
a=box[x]
b=box[y]
print(a,b)
if a[0]==b[0] and a[0]=="白" :
win=win+1
else:
lose=lose+1
i=i+1
print('赢',win,'输',lose)
继续优化,删除多余内容,计算结果
[Python] 纯文本查看复制代码
import random
win=0
lose=0
box=('白1','白2','白3','黑4','黑5','黑6')
z=int(input('请输入试验次数:'))
for i in range(z):
x=random.randint(0,5)
y=random.randint(0,5)
a=box[x]
b=box[y]
# print(a,b)
if a[0]==b[0] and a[0]=="白" :
win=win+1
else:
lose=lose+1
i=i+1
print('赢',win,'赢',lose)
m=win/z
n=lose*3-win*7
print("中奖概率是{:.3f},最后获利{}".format(m,n))
三、记录计算机处理数据的时间
10万级的数据基本在0.5s左右(具体的因计算机处理能力而定)
[Python] 纯文本查看复制代码
import random,time
win=0
lose=0
box=('白1','白2','白3','黑4','黑5','黑6')
z=int(input('请输入试验次数:'))
start_time = time.time() #注意计时 代码放的位置
for i in range(z):
x=random.randint(0,5)
y=random.randint(0,5)
a=box[x]
b=box[y]
# print(a,b)
if a[0]==b[0] and a[0]=="白" :
win=win+1
else:
lose=lose+1
i=i+1
end_time = time.time()
print('赢',win,'赢',lose)
m=win/z
n=lose*3-win*7
print("中奖概率是{:.3f},摊主最后获利{}".format(m,n))
total_time = end_time - start_time
print("*****运算耗时{:.4}秒*****".format(total_time))
import random,time
from collections import Counter
win=0
lose=0
box=['红']*6+['绿']*6+['蓝']*6
win_dict={
'843':300,
'831':200,
'822':100,
'750':150,
'741':100,
'732':50,
'660':50,
'642':30,
'552':80,
'543':-50,
'552':60,
'444':180,
}
# print(box)
z=int(input('请输入试验次数:'))
start_time = time.time()
for i in range(z):
x=list(Counter(random.choices(box,k=12)).values()) #随机选12个 并计算每个元素的个数
x.sort(reverse=True) #排序从大到小
result = "".join([str(j) for j in x])
result = '750' if result=='75' else result
result = '660' if result=='66' else result
result = '840' if result=='84' else result
if result in win_dict.keys():
print(result,win_dict[result])
if win_dict[result]>0:
win=win + win_dict[result]
else:
lose+= win_dict[result]
end_time = time.time()
print('胜',win,'负',lose)
total_time = end_time - start_time
print("*****运算耗时{:.4}秒*****".format(total_time))