0

我在使用 1 行代码时遇到问题,我一遍又一遍地运行它,但我不知道我做错了什么,我认为这是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")
4

2 回答 2

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"

此外,您还有很多与您如何使用 print 相关的小事情,您应该整理一下,这是您原始 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"
于 2013-09-12T18:03:41.583 回答
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
于 2013-09-12T18:17:42.913 回答