Question

Post can be closed. Turns out I messed up something unknown, because now I retried and the int() function is working as I wanted.

`

`

This is a small piece of code that I wrote:

def damagecalculating(self):
        self.damage = random.randrange(1,100) * self.weapondamage / 5
        self.damage = int(self.damage)

(used long words here to make clear what I am doing)

So what I'm doing is calculating the damage a player does in an attack. but I want an integer and not a float so had to add the extra line.

This returns a float for some reason:

self.damage = int((random.randrange(1,100) * self.weapondamage) / 5)

I don't understand that, because what I see is that random.randrange(1,100) is calculated, then self.wepdamage is found and thus the formula becomes, for example: 55 * 10 / 5.

Why would this return a float in the first place (I found it has something to do with the /)? And why does int() not work? because int(55*10/5) does return an integer.

I did already find http://www.stackoverflow.com/questions/17202363/int-conversion-not-working but this does not answer my question as to why or if this can be done in one line.

Edit:

I don't really see why this would be needed but this is the full code as some requested. Please note that I'm very new to programming and there is probably a bunch of stuff that can be done better. Also it's far from done.

import random

class playerstats:
    def usepotion(self):
        heal = random.randrange(100,500)
        self.health = self.health + heal
        self.pots -= 1
        print('The potion healed', str(heal) +'!')
        print(self.name, 'now has', str(self.health), 'health.')

    def minushealth(self, amount):
        self.health = self.health - amount

    def damagecalc(self):                    ### HERE IS THE PROBLEM ###
        self.damage = random.randrange(1,100) * self.wepdamage / 5
        self.damage = int(self.damage)


    name = ''
    health = 0
    weapon = ''
    wepdamage = 5
    damage = 0
    pots = 0


def printdata():
    print(player.name, 'health =', player.health)
    print(player.name, 'potions =', player.pots)
    print()

    print(opp.name, 'health =', opp.health)
    print(opp.name, 'potions =', opp.pots)
    print()

def setgamedata():
    player.name = input('Set player name: ')
    player.health = int(input('Set player health (1000): '))
    player.weapon = input('Set player weapon name: ')
    player.wepdamage = int(input('Set player damage multiplier (5 = normal): '))
    player.pots = int(input('Set number of potions for player: '))

    print()

    opp.name = input('Set opponent name: ')
    opp.health = int(input('Set opponent health (1000): '))
    opp.weapon = input('Set opponent weapon name: ')
    opp.wepdamage = int(input('Set opponent damage multiplier (5 = normal): '))
    opp.pots = int(input('Set number of potions for opponent: '))

    print()

def resetgamedata():
    player.name = input('Player name currently is: ' + player.name + '. Set player name: ')
    player.health = int(input('Player health currently is: ' + str(player.health) + '. Set player health (1000): '))
    player.weapon = input('Player weapon currently is', player.weapon, 'Set player weapon name: ')
    player.wepdamage = int(input('Player damage multiplier currently is: ' + str(player.wepdamage) + '. Set player damage multiplier: '))
    player.pots = int(input('Player currently has ' + str(player.pots) + ' potions. Set player potions: '))

    print()

    opp.name = input('Opponent name currently is: ' + opp.name + '. Set opponent name: ')
    opp.health = int(input('Opponent health currently is: ' + str(opp.health) + '. Set opponent health (1000): '))
    opp.weapon = input('Opponent weapon currently is', opp.weapon, 'Set opponent weapon name: ')
    opp.wepdamage = int(input('Opponent damage multiplier currently is: ' + str(opp.wepdamage) + '. Set opponent damage multiplier: '))
    opp.pots = int(input('Opponent currently has ' + str(opp.pots) + ' potions. Set opponent potions: '))

    print()

def menuoptions():
    print('1. Start new game')
    print('9. Quit')
    print()

def battleoptions():
    print('1. Attack')
    print('2. Use potion')
    print('3. Change stats')
    print('9. Abandon game')
    print()



### ACTUAL GAME CODE STARTS HERE ###

# Set objects
player = playerstats()
opp = playerstats()

print('- - - - - - - - - - - - - - - -\n')
print('Welcome to the game!\n\n')
print('Entering main menu\n')


while True:
    menuoptions()

    choice=int(input('Enter number: '))
    print()

    while True:
        if choice == 1:
            setgamedata()

            print('Starting game now!')

            while player.health > 1 and opp.health > 1:
                battleoptions()

                choice = int(input('Enter number: '))
                print()

                # Execute player move
                if choice == 1:
                    printdata()

                elif choice == 2:
                    player.usepotion()

                elif choice == 3:
                    resetgamedata()

                elif choice == 9:
                    print('Quit game')
                    input('Press enter to return to main screen')
                    break

                else:
                    print('No valid choice made')

                # Execute opponent move
                if opp.health < 200:
                    oppmove = 1

                else:
                    oppmove = 0


                if oppmove == 1:
                    opp.usepotion()

                else:
                    print('nothing here')
                    ##### ATTACK PLAYER

            ### SOMETHING HERE WHEN PERSON REACHED < 0 HEALTH


        if choice == 9:
            print('\nQuit?! Okay fine\n')
            print('Your stuff was not saved, good luck with that.\n')
            input('Press enter to close screen')
            import sys
            sys.exit()









input('')
Was it helpful?

Solution

The issue you are experiencing is that in Python 3.x the division operator / performs true division - that is, the returned value is float. If you want the behaviour of Python 2.x, that is integer division use the // operator. Note, that the // division result will be int if the dividend and divisor are integers.

>>> 5 / 10
0.5
>>> 5 // 10
0
>>> type(5 // 10)
<class 'int'>

For floats, the // operator still returns a float:

>>> 5.0 // 10
0.0
>>> 5 // 10.0
0.0
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top