2014-04-07 73 views
0

我需要一些幫助這個代碼!它基本上允許用戶創建兩個角色並輸入技能值和強度值。它通過從最大值中減去最小值來創建每個修改器。兩個骰子是爲每個角色滾動的,最高的滾輪會將修改者添加到他的數值中並減去最低值。然後,如果力量小於或等於零,角色就會死亡。但是,這是行不通的,他們都生存下來,即使強度小於0Python骰子游戲不按預期方式工作

任何幫助,將不勝感激

import random 
c1 = str(input("First Character Name: ")) 
c1st = int(input("Strength: ")) 
c1sk = int(input("Skill: ")) 
c2 = str(input("\nSecond Character Name: ")) 
c2st = int(input("Strength: ")) 
c2sk = int(input("Skill: ")) 
strengthmod = max(c1st, c2st) - min(c1st, c2st) 
skillmod = max(c1sk, c2sk) - min(c1sk, c2sk) 
c1roll = random.randint(1,6) 
c2roll = random.randint(1,6) 
print('\n'+ c1 +' rolled a ' + str(c1roll)) 
print(c2 +' rolled a ' + str(c2roll)) 

if c1roll > c2roll: 
    c1st = c1st + strengthmod 
    c1sk = c1sk + skillmod 
    c2st = c2st - strengthmod 
    c2sk = c2sk - skillmod 
elif c2roll > c1roll: 
    c2st = c2st + strengthmod 
    c2sk = c2sk + skillmod 
    c1st = c1st - strengthmod 
    c1sk = c1sk - skillmod 

if c1st <= 0: 
    if c2st <= 0: 
     print(c1 + c2 +" have both died in battle") 
     c1st == 0 
     c2st == 0 
    else: 
     print(c1 + " died in combat") 
     c1st == 0 
elif c2st <= 0: 
    print(c2 + " died in combat") 
c2st == 0 
    print("\nBoth had the strength to survive the fight\n") 

print(c1+'s Strength is at '+str(c1st) +" and skill is " + str(c1sk)) 
print(c2+'s Strength is at '+str(c2st) +" and skill is " + str(c2sk)) 
+0

我不明白爲什麼當任'c1st'或'c2st'變爲0或低於您的代碼將無法正常工作。你的'c1st == 0'行不是0,它們測試的值是否等於* 0(然後放棄測試),但是這些都是相對無害的,而不是你的問題的原因。 –

+0

你的'c2st == 0'行(在'elif'後面)缺少'if',否則你會在下一個print()行出現縮進錯誤。 –

+0

當'strengthmod = abs(c1st - c2st)'更容易編寫時,不要使用'strengthmod = max(...) - min(...)'。 :-) –

回答

1

你只需要一個else添加到if/elif。就目前而言,「兩條生存線」與「c2在戰鬥中死亡」的代碼塊相同,這並沒有多大意義。

另外,我想你想用c2st = 0而不是c2st == 0,即將強度設置爲零,如果它低於零。

if c1st <= 0: 
    ... 
elif c2st <= 0: 
    print(c2 + " died in combat") 
    c2st = 0 
else: 
    print("\nBoth had the strength to survive the fight\n")