A very peculiar problem


#1

Why won't it divide 2 variables?

import time
import random

print "Opening Realm..."
time.sleep(1);
print "Realm open"
time.sleep(0.2);
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)
    else:
        print "Sir, our area is clear."

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


#Enemy Army END

#2

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


#3

Sorry, its there now.


#4

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


#5

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

chancesOfWin = float(ourArmyCount/(enemyArmyCount) * 100

#6

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