2014-05-13 179 views
-1

帖子可以關閉。事實證明,我搞砸了一些未知的東西,因爲現在我重試了,int()函數正在按照我的要求工作。爲什麼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

爲什麼這會返回一個浮動首位(我發現它與/有關)?爲什麼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('') 
+6

否,'INT((random.randrange(1,100)* self.weapondamage)/ 5)'將**從來沒有**返回一個浮動。也許你錯誤地輸入了'int(random.randrange(1,100)* self.weapondamage)/ 5'來代替? –

+0

確定你不會在這行後面再次修改'self.damage',或者變量名稱中沒有拼寫錯誤? –

+0

在Python 3中'/'除法運算符總是會導致浮點數。 –

回答

0

您所遇到的問題是,在Python 3.x中除法運算/執行真正的除法 - 也就是說,返回的值是浮動。如果你想要Python 2.x的行爲,那就是整數除法,使用//運算符。請注意,如果除數和除數是整數,則//除法結果將爲int。

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

對於浮筒,所述操作者//仍然返回float:

>>> 5.0 // 10 
0.0 
>>> 5 // 10.0 
0.0 
+0

哇,在代碼和Google搜索頁面上打破了我的想法。答案是'寫//代替/'。非常感謝。 – LuukV

+0

@kroolik我不明白這是如何解決這個問題,爲什麼'self.damage = int(self.damage)'不起作用? –

+0

'self.damage = int(self.damage)'工作。問題是'int((random.randrange(1,100)* self.weapondamage)/ 5)'沒有返回一個整數。所以答案是寫「// 5」 – LuukV