我是Python的初学者,我决定通过创建一个具有概率的骰子程序来挑战自己。它非常简单和混乱,但代码可以在下面看到:
import random
rolled = []
rolledtimes = 0;
biggest = []
freq = int(input('How many times would you like to roll the dice? '))
def roll():
rand = random.randrange(1,7)
return rand
def probability():
for i in range(0,6):
print('Calculation of probability:')
percentage = "{:.2f}".format((count[i] / freq)*100)
percent = str(percentage) + '%'
print(' ', i + 1, ':', percent)
def theoretical():
result = "{:.2f}".format((1/6)*freq)
denominator = "{:.2f}".format(((1/6)*freq)*6)
print('\nIn theory, each dice should roll {} out of {} times'.format(result,denominator))
def findBiggest():
for i in range(1,7):
biggest.append(rolled.count(i))
print('\n', 'The most times a dice is rolled is', max(biggest), 'times')
def findSmallest():
for i in range(1,7):
biggest.append(rolled.count(i))
print('\n', 'The least times a dice is rolled is', min(biggest), 'times')
for i in range(1,freq + 1):
number = roll()
rolled.append(number)
rolledtimes+=1
count = [rolled.count(1),rolled.count(2),rolled.count(3),rolled.count(4),rolled.count(5),rolled.count(6)]
print('After being rolled {} times:\n\n1 is rolled {} times\n2 is rolled {} times\n3 is rolled {} times\n4 is rolled {} times\n5 is rolled {} times\n6 is rolled {} times\n'.format(rolledtimes,count[0],count[1],count[2],count[3],count[4],count[5]))
probability()
findBiggest()
findSmallest()
theoretical()
即使它很乱并且非结构化,我还是设法让它在PyCharm程序上运行(我正在使用它)。完成输入后,这就是我的控制台的样子:
How many times would you like to roll the dice? 1000
After being rolled 1000 times:
1 is rolled 161 times
2 is rolled 155 times
3 is rolled 188 times
4 is rolled 155 times
5 is rolled 173 times
6 is rolled 168 times
Calculation of probability:
1 : 16.10%
Calculation of probability:
2 : 15.50%
Calculation of probability:
3 : 18.80%
Calculation of probability:
4 : 15.50%
Calculation of probability:
5 : 17.30%
Calculation of probability:
6 : 16.80%
The most times a dice is rolled is 188 times
The least times a dice is rolled is 155 times
In theory, each dice should roll 166.67 out of 1000.00 times
Process finished with exit code 0
因为这似乎有效,我尝试使用Python的程序IDLE和Python启动程序运行它,但控制台中的结果似乎有所不同。当我在IDLE上运行程序时,该程序似乎无法正常工作:
How many times would you like to roll the dice? 1000
After being rolled 1000 times:
1 is rolled 182 times
2 is rolled 180 times
3 is rolled 156 times
4 is rolled 167 times
5 is rolled 163 times
6 is rolled 152 times
Calculation of probability:
(' ', 1, ':', '0.00%')
Calculation of probability:
(' ', 2, ':', '0.00%')
Calculation of probability:
(' ', 3, ':', '0.00%')
Calculation of probability:
(' ', 4, ':', '0.00%')
Calculation of probability:
(' ', 5, ':', '0.00%')
Calculation of probability:
(' ', 6, ':', '0.00%')
('\n', 'The most times a dice is rolled is', 182, 'times')
('\n', 'The least times a dice is rolled is', 152, 'times')
In theory, each dice should roll 0.00 out of 0.00 times
Exit status: 0
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[Process completed]
如何让我的程序使用python的IDLE?它适用于Python的控制台,但不适用于IDLE。请帮助!
答案 0 :(得分:1)
Pycharm正在运行python 3和idle python 2,如打印和除法操作的差异所示。
要在第一行添加相同的内容,请添加:
from __future__ import (print_function, division)
在python 2中,print是一个命令,而在python 3中它是一个函数,所以:
print(1, 2)
- > 1 2
py3 或(1, 2)
py2
和python 2整数除法总是得到一个整数
3/6
- > 0.5
py3 或0
py2