Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python (PyCharm) console outcome different from python IDLE outcome

Tags:

python

I'm a begginer in Python, and I decided to challenge myself by creating a dice program with probability. It is very simple and messy, but the code could be seen below:

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()

Even though it was messy and unstructured, I managed to get it working on the PyCharm program (which I am using). After doing the inputs, this is what my console looks like:

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

Because this seems to be working, I tried to run it using Python's program IDLE, and the Python launcher, but the outcome in the console seems to be different. The program doesn't seem to be working when I run it on 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]

How do I get my program to work on python's IDLE? It works on Python's console but not on IDLE. Please Help!

like image 641
raven rogue Avatar asked Nov 22 '25 16:11

raven rogue


1 Answers

Pycharm is running python 3 and idle python 2 as shown by the differences in print and divide operations.

To get it the same on both on the first line add:

from __future__ import (print_function, division)

In python 2 print is a command while in python 3 it is a function so:

print(1, 2) -> 1 2 py3 or (1, 2) py2

and python 2 integer division always results in an integer so 3/6 -> 0.5 py3 or 0 py2

like image 179
Steve Barnes Avatar answered Nov 25 '25 05:11

Steve Barnes