2014-02-28 22 views
0
import random 
import types 

attribute = types.SimpleNamespace() 
attribute.C1strength= "10" 
attribute.C1skill="10" 
attribute.C2strength="10" 
attribute.C2skill="10" 
attribute.counter="" 

attribute.counter=1 

characterNameOne=str(input("Please input first character's name"))  
characterNameTwo=str(input("Please input second character's name")) 
print("The characters have 2 attributes : Strength and Skill") 
print ("A 12 and 4 sided dice are rolled") 
print("Each character is set to 10") 
print("The number rolled on the 12 sided dice is divided by the number rolled on the 4 sided dice") 
print ("The value of the divided dice is added to the character's attributes") 

for counter in range (attribute.counter): 
    dieOne = random.randint(1,4) 
    dieTwo = random.randint(1,12) 
    divisionValue= dieTwo/dieOne 
    divisionValue= round(divisionValue,0) 
    x=int(divisionValue) 
    int(x) 
    x=(x-0) 

    print(x) 

    attribute.C1strength = (attribute.C1strength + str(x)) 
    print(characterNameOne) 
    print("Your strengh value is :",attribute.C1strength) 

    attribute.C1strength += str(x) 
    print("Your skill value is :",attribute.C1skill) 


    attribute.C1strength += str(x) 
    print(characterNameTwo) 
    print("Your strengh value is :",attribute.C2strength) 

    attribute.C1strength += str(x) 
    print("Your skill value is :",attribute.C2skill) 

fileObj = open("CharacterAttributes.txt","w") 
fileObj.write('Charcter one ,your strength:' + str(attribute.C1strength) + '\n') 
fileObj.write('Character one, your skill:' + str(attribute.C1skill) + '\n') 
fileObj.write('Character two),your strength:' + str(attribute.C2strength) + '\n') 
fileObj.write('Characte two), your skill:' + str(attribute.C2skill) + '\n') 
fileObj.close() 

你好,這是我的代碼, 我想要的人物屬性,開始在10,然後(divisionValue)加入。我唯一的問題是我想分割值被添加到每個字符的屬性,它增加了數字,如10 + 1 =等於101. 此外,分割值應該是不同的,每次都應該調用該過程它需要時間。對不起,如果我的代碼錯了,我剛開始使用python。謝謝 !循環的代碼的某些部分在循環中,並加入simplenamespace和INT

回答

0

發生這種情況是因爲您正在添加字符串而不是數字。只要確保使用初始力量和技能值的整數,並且在更新這些值時不要將str()調用應用到x

1

爲了連接兩個整數,你會做以下

x = 10 
y = 1 
z = str(x) + str(y) 

,這將給你「101」,如果你需要把它看成一個int,只是做

z = int(str(x) + str(y)) 

不太確定你的問題後半部分的含義。

您可能想要將您的for循環包裝在函數中,並將其傳遞給divisionValue。這將允許您使用不同的divisionValue值多次調用它。

+0

謝謝,這幫了很大忙。 – user3357232