Python(PyCharm)控制台结果与python空闲结果不同
我是Python中的一个begginer,我决定用概率创建一个dice程序来挑战自己。这是非常简单而凌乱,但代码可以在下面看到:Python(PyCharm)控制台结果与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的发射,但在控制台中的结局似乎不一样。该方案似乎并不奏效,当我在怠速运转:
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的无功?它适用于Python的控制台,但不适用于IDLE。请帮忙!
Pycharm正在运行python 3和idle python 2,如打印和除法操作的差异所示。
为了得到它同在两个上的第一行加载:
from __future__ import (print_function, division)
在Python 2打印是一个命令,而在python 3是一个函数,以便:
print(1, 2)
- >1 2
PY3或(1, 2)
PY2
和python 2整数除法总是导致的整数所以 3/6
- >0.5
PY3或0
PY2
这解决了它,谢谢! –
@ravenrogue然后请标记为答案。 –
使用在Python 3.x的IDLE –
我得到了NameError:名称'滚动'没有定义,什么是滚动? – MishaVacic
请确保您使用的是相同的Python版本(即,如果python3作为pycharm解释器,然后闲置python3) – Gahan