第一个文字游戏......无法找出药水的代码

时间:2013-09-12 18:00:03

标签: python python-2.7

我遇到一行代码遇到问题我一遍又一遍地运行它但是我不知道我做错了什么,我认为这是if声明但是我我不确定。我遇到问题的那条线就在评论中。我不知道怎么写它才能正常工作。

import random

print"Hello hero, you are surrounded by a horde of orcs"
print"that span as far as the eye can see"
print"you have 5 health potions, 200 health"
print"you can either fight, run, or potion"

h=200 #health
w=0 #wave
o=0 #orc
p=5 #potion

while h>=0:
    print"you run into a horde of orcs what will you do?"
    a=raw_input("fight, run, potion")
    if a=="fight":
        print"you draw your sword and begin to fight"
        d=random.randint(1, 20)
        w+=1
        o+=1
        p=5
        h-=d
        print"you survive this horde"
        print("you have, "+str(h)+" health left")
    elif a=="potion":
        print"you have used a potion"
        h+=20
        p=p-1
        print"you have recovered 20 health"
        print("you now have, "+str(h)+" health")
        if p==0: #supposed to be if potion = 0 print (you dont have any left)
            print"you dont have any left"
    elif a=="run":
        h-=5
        print"you got away but suffered 5 damage"
        print("you have, "+str(h)+" health left")
print"you  have fought well but alas hero you were defeated"
print("you survived, "+str(w)+" waves and have killed, "+str(o)+" orcs")

2 个答案:

答案 0 :(得分:1)

我认为你想要在使用之前测试是否有任何药水。像这样:

elif a=="potion":
    if p==0: #supposed to be if potion = 0 print (you dont have any left)
        print"you dont have any left"    
    else:
        print "you have used a potion"
        h+=20
        p=p-1
        print "you have recovered 20 health"
        print "you now have, "+str(h)+" health"

此外,你有很多关于如何使用打印的小事,你应该理清,这是你原来的Python脚本的清理版本(我测试过,它似乎有效,祝你好运游戏:)

import random

print "Hello hero, you are surrounded by a horde of orcs"
print "that span as far as the eye can see"
print "you have 5 health potions, 200 health"
print "you can either fight, run, or potion"

h=200 #health
w=0 #wave
o=0 #orc
p=5 #potion

while h>=0:
    print "you run into a horde of orcs what will you do?"
    a=raw_input("fight, run, potion")
    if a=="fight":
        print "you draw your sword and begin to fight"
        d=random.randint(1, 20)
        w+=1
        o+=1
        p=5
        h-=d
        print "you survive this horde"
        print "you have, "+str(h)+" health left"
    elif a=="potion":
        if p==0: #supposed to be if potion = 0 print (you dont have any left)
            print "you dont have any left"
        else:
            print "you have used a potion"
            h+=20
            p=p-1
            print "you have recovered 20 health"
            print "you now have, "+str(h)+" health"
    elif a=="run":
        h-=5
        print "you got away but suffered 5 damage"
        print "you have, "+str(h)+" health left"

print "you  have fought well but alas hero you were defeated"
print "you survived, "+str(w)+" waves and have killed, "+str(o)+" orcs"

答案 1 :(得分:0)

这是您应该添加到代码中的内容:

elif a=="potion":
    if p>0:
        print"you have used a potion"
        h+=20
        p=p-1
        print"you have recovered 20 health"
        print("you now have, "+str(h)+" health")
    else: #supposed to be if potion = 0 print (you dont have any left)
        print"you dont have any potion left"
        continue