import random
f1,f2,f3,f4,f5,f6=0,0,0,0,0,0for _ inrange(60000):# 下划线作为变量名(用不到变量名)
face=random.randrange(1,7)if face:
f1+=1elif face ==2:
f2+=1elif face ==3:
f3+=1elif face ==4:
f4+=1elif face ==5:
f5+=1else:
f6+=1print(f'1点掷出了{f1}次')print(f'2点掷出了{f2}次')print(f'3点掷出了{f3}次')print(f'4点掷出了{f4}次')print(f'5点掷出了{f5}次')print(f'6点掷出了{f6}次')
如果上面的例子掌握了,那么不妨试试下面这个升级版:
题干:将两颗骰子同时掷60000次,统计这两颗骰子每面出现的次数只和
import random
f2=f3=f4=f5=f6=f7=f8=f9=f10=f11=f12=0for _ inrange(60000):# 下划线作为变量名(用不到变量名)
face1=random.randrange(1,7)
face2=random.randrange(1,7)
face = face1 + face2
if face ==2:
f2 +=1elif face ==3:
f3 +=1elif face ==4:
f4 +=1elif face ==5:
f5 +=1elif face ==6:
f6 +=1elif face ==7:
f7 +=1elif face ==8:
f8 +=1elif face ==9:
f9 +=1elif face ==10:
f10 +=1elif face ==11:
f11 +=1else:
f12 +=1print(f'2点掷出了{f2}次')print(f'3点掷出了{f3}次')print(f'4点掷出了{f4}次')print(f'5点掷出了{f5}次')print(f'6点掷出了{f6}次')print(f'7点掷出了{f7}次')print(f'8点掷出了{f8}次')print(f'9点掷出了{f9}次')print(f'10点掷出了{f10}次')print(f'11点掷出了{f11}次')print(f'12点掷出了{f12}次')