获取TypeError:无法隐式地将'float'对象转换为str?

时间:2014-03-25 14:51:53

标签: python

这是我的GCSE计算代码项目的一部分,我无法让它正常工作,请帮助我。 它意味着成为一个“遭遇者”。两个角色之间的游戏

import random

tryagain = "Y" or "y"


char1name = input(str("character 1 please enter your name "))
char2name = input(str("character 2 please enter your name "))

print ("Player 1, your name is " + char1name)
print ("Player 2, your name is " + char2name)
print ("  ")
player1skill = input(str("what is your skill level " + char1name + "? "))
player2skill = input(str("what is your skill level " + char2name + "? "))
player1strength = input(str("what is your strength level " + char1name + "? "))
player2strength = input(str("what is your strength level " + char2name + "? "))

print (char1name + " has a skill of " + player1skill + " and a strength of " +  player1strength)
print (char2name + " has a skill of " + player2skill + " and a strength of " +  player2strength)

if (int(player1strength) >= int(player2strength)):
    strength_modifier = ((int(player1strength)) - (int(player2strength)))
else:
    strength_modifier = (int(player2strength) - (int(player1strength)))

if (int(player1skill) >= int(player2skill)):
    skill_modifier = ((int(player1skill)) - (int(player2skill)))
else:
    skill_modifier = (int(player2skill) - (int(player1skill)))
print("The strength modifier is " + (int(strength_modifier) / (int(5))))
print("The skill modifier is " + (int(skill_modifier) // (int(5))))

roll_player1 = random.randint(1,6)
roll_player2 = random.randint(1,6)

if int(roll_player1) == int(roll_player2):
    print("no changes are made")
else: print(char1name +"'s skill has gone up to " + (str(player1skill) +  str(skill_modifier)))
print("and their strength has gone up to" + (int(player1strength) + int(strength_modifier)))
if int(roll_player1) <= int(roll_player2):
    print(char2name + "'s skill has gone up to " + (int(player2skill) +  int(skill_modifier)))
    print("and their strength has gone up to" + (int(player2strength) +       int(strength_modifier)))

这是我不断得到的错误,问题是什么?

character 1 please enter your name player1
character 2 please enter your name player2
Player 1, your name is player1
Player 2, your name is player2

what is your skill level player1? 10
what is your skill level player2? 5
what is your strength level player1? 20
what is your strength level player2? 10
player1 has a skill of 10 and a strength of 20
player2 has a skill of 5 and a strength of 10
Traceback (most recent call last):
  File "\\fileserver-01\studenthome$\*serverlocation*
 line 29, in <module>
    print("The strength modifier is " + (int(strength_modifier) / (int(5))))
TypeError: Can't convert 'float' object to str implicitly

1 个答案:

答案 0 :(得分:1)

此操作的结果:

(int(strength_modifier) / (int(5)))

浮动。要将其更改为字符串,只需将其转换为字符串。

str(int(strength_modifier) / (int(5)))