2014-03-25 43 views
-1

这是我的GCSE计算代码项目的一部分,我不能让它正常工作,请帮助我。 是指两个字符获取TypeError:不能将'float'对象隐式转换为str?

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 
+0

已回答 - http://stackoverflow.com/questions/13654168/typeerror-cant-convert-int-object-to-str-implicitly?rq=1 – jaime

回答

1

这个操作的结果:

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

是float。要将其更改为字符串,只需将其转换为字符串即可。

str(int(strength_modifier)/(int(5))) 
+0

的两个师'int'返回'float'? – CoryKramer

+0

@Cyber​​在Python3它确实 –

+0

@Cyber​​是的,显然是如此。 Python 3. – aIKid

相关问题