2014-03-25 67 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

相關問題