为什么Int()转换不起作用?

时间:2014-05-13 11:28:53

标签: python python-3.x

发布可以关闭。事实证明我弄乱了一些未知的东西,因为现在我重试了,int()函数正在按我的意愿工作。

`

`

这是我写的一小段代码:

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

(在这里使用长词来说明我在做什么)

所以我正在做的是计算玩家在攻击中造成的伤害。但是我想要一个整数而不是浮点数,所以不得不添加额外的行。

由于某种原因,这会返回一个浮点数:

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

我不明白,因为我看到的是random.randrange(1,100)被计算出来,然后找到self.wepdamage,因此公式变为,例如:55 * 10 / 5。< / p>

为什么这会首先返回一个浮点数(我发现它与/有关)?为什么int()不起作用?因为int(55*10/5)确实返回一个整数。

我确实找到了http://www.stackoverflow.com/questions/17202363/int-conversion-not-working,但这并没有回答我的问题,为什么或是否可以在一行中完成。

编辑:

我真的不明白为什么会这样做,但这是一些要求的完整代码。请注意,我对编程非常陌生,可能有很多东西可以做得更好。它还远未完成。

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

1 个答案:

答案 0 :(得分:0)

您遇到的问题是,在Python 3.x中,除法运算符/执行真正的除法 - 即返回的值为float。如果你想要Python 2.x的行为,那就是整数除法使用//运算符。注意,如果被除数和除数是整数,则//除法结果将为int。

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

对于浮点数,//运算符仍然返回浮点数:

>>> 5.0 // 10
0.0
>>> 5 // 10.0
0.0
相关问题