帖子可以关闭。事实证明,我搞砸了一些未知的东西,因为现在我重试了,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('')
否,'INT((random.randrange(1,100)* self.weapondamage)/ 5)'将**从来没有**返回一个浮动。也许你错误地输入了'int(random.randrange(1,100)* self.weapondamage)/ 5'来代替? –
确定你不会在这行后面再次修改'self.damage',或者变量名称中没有拼写错误? –
在Python 3中'/'除法运算符总是会导致浮点数。 –