A very peculiar problem


Why won't it divide 2 variables?

import time
import random

print "Opening Realm..."
print "Realm open"
print "Welcome"

time = 24;
#Enemy Army BEGIN
ourArmyCount = 500
attacks = [6, 7]
army = range(20, 5000)

def attacking():
    attackDay = random.choice(attacks)
    if attackDay == 7:
        print "An army from far away is approaching Sir."
        enemyArmyCount = random.choice(army)
        chancesOfWin = (ourArmyCount / enemyArmyCount) * 100
        print "Enemy Army Stand: %s" % (enemyArmyCount)
        print "Our Arrmy Stand: %s" % (ourArmyCount)
        print "Chances of Win: %s " % (chancesOfWin)
        print "Sir, our area is clear."

while (time == 24):
    time = 0;

#Enemy Army END


can you actually copy paste the code to the forum so i can run it?


Sorry, its there now.


no problem, okay i found the problem, i think. i ran the program an i got for example:

Enemy Army Stand: 2434
Our Arrmy Stand: 500

so then you do: 500/2434. The problem in python2 is that the number is floored (a integer divided by a integer gives a integer as result, never a float) (float = decimal number, integer = non-decimal number)

You could overcome the problem by doing:

chancesOfWin = float(ourArmyCount) / enemyArmyCount * 100

no need to use further brackets because of order of operations


Thank you :smiley: Tried using float like in the code below, but messed it up. :c

chancesOfWin = float(ourArmyCount/(enemyArmyCount) * 100


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.