我是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。請幫忙!
使用在Python 3.x的IDLE –
我得到了NameError:名稱'滾動'沒有定義,什麼是滾動? – MishaVacic
請確保您使用的是相同的Python版本(即,如果python3作爲pycharm解釋器,然後閒置python3) – Gahan