vendredi 21 décembre 2018

Python: Why do my 'if' statements' evaluate numbers incorrectly?

The code is as follows:

from random import *

numStars = int(input("Number of stars: ").strip())

planetTypeDict = {'O':0, 'B':0, 'A':0, 'F':0, 'G':0, 'K':0, 'M':0}

for star in range (numStars):
    planetTypeChanceNum =  uniform (0, 100)

    if planetTypeChanceNum < 76.45:
        planetType = 'M'
        planetTypeDict['M'] += 1

    elif planetTypeChanceNum < 12.1:
        planetType = 'K'
        planetTypeDict['K'] += 1

    elif planetTypeChanceNum < 7.6:
        planetType = 'G'
        planetTypeDict['G'] += 1

    elif planetTypeChanceNum < 3:
        planetType = 'F'
        planetTypeDict['F'] += 1

    elif planetTypeChanceNum < 0.6:
        planetType = 'A'
        planetTypeDict['A'] += 1

    elif planetTypeChanceNum < 0.13:
        planetType = 'B'
        planetTypeDict['B'] += 1

    elif planetTypeChanceNum < 0.0003:
        planetType = 'O'
        planetTypeDict['O'] += 1

print(planetTypeDict)

The problem is that the code outputs that there are about 75% 'M' stars and nothing else besides. For example, when i use 1000 as the value for numStars, I get a result of about:

{'O': 241, 'B': 0, 'A': 0, 'F': 0, 'G': 0, 'K': 0, 'M': 759}

I have tried as much as I can to fix this error, including changing the logic to:

if planetTypeChanceNum > 100 - 'chance here':
    # stuff

Please help!

Aucun commentaire:

Enregistrer un commentaire