Python中基本while循环的问题

时间:2015-07-19 02:14:24

标签: python while-loop

我正致力于一个基本的文字游戏,以提高我的python技能。我有一个部分,其中有一系列if语句,我想使用while循环,以防人们使用给定3之外的选择,然后它将循环回到开始并再次询问它们。

出于某种原因,它打印出来并没有计算,请再试一次'即使给出了正确的1-3选择,我也无法弄清楚原因。我一直在学习如何做到这一点,我所做的似乎与那个结构相同,所以不确定我做错了什么......

def enter(self):
    print "You move down into the east tunnel"
    print "You walk down the tunnel for what seems like a long time, there are flickering torches along the wall that give off small amounts of light but its still"
    print "very difficult to see anything ahead"
    print "You hear a growling and shuffling noise ahead.....as you peer into the gloom you see a large troll!"
    print "what will you do?"

print """1 - draw your sword and fight him.
         2 - try to run past him.
         3 - use one of your objects.
         """

troll_choice = raw_input("> ")

while troll_choice != "1" or troll_choice != "2" or troll_choice != "3":
    print "doesnt compute please try again"

    troll_choice = raw_input("> ")

if troll_choice == "1":
    print "you will fight the troll"
elif troll_choice == "2":
    print "The troll looks slow and stupid but not as slow as you think! As you try to rush past him he brings his giant club down on top of your head"
    print "killing you instantly"
    return 'death'

elif troll_choice == "3":
    print "which of your items will you use?"
    items_choice = raw_input("> ")
    if items_choice == "matches":
            print "You fool, matches are useless against a creature of this size, he slays you swiftly"
            return 'death'

    elif items_choice == "gold coin":
            print "you flick the gold coin at the Troll, the troll looks puzzled and watches the shiny coin arch through the air, no is your chance to attack!"
            print "the Troll's defences are down, you draw your sword and lunge towards its soft underbelly and drive your sword home killing it instantly"
            print "you wipe the blood from your sword and head on down the tunnel"

            return 'room_b'

    elif items_choice == "flashbang dust" or items_choice == "flashbang":
            print "You pull out the puch of flashbang dust and throw it hard on the floor at the trolls feet while shielding your eyes..."
            print "the blast and white light from the explosion blinds the troll, it roars and covers its eyes..."
            print "you decide to use this time to get past the troll and sprint down the tunnel leaving him behind"

            return 'room_b'

    elif items_choice == "silver dagger" or items_choice == "iron cross":
            print "Why would these be of any use against a troll??"
            print "he quickly slays you!"

            return 'death'

2 个答案:

答案 0 :(得分:2)

while循环继续循环,直到条件变为false,即您给出的条件 -

while troll_choice != "1" or troll_choice != "2" or troll_choice != "3":

永远不会变错,因为troll_choice不能同时是1以及23。所以它会一直无限期地看。

你想在条件而不是and中使用or,以便在troll_choice成为三者之一时打破循环。

但更好的方法是使用in opertor -

while troll_choice not in ["1" , "2" , "3"]:

答案 1 :(得分:0)

对不起,我无法成功运行您的程序,可能是错误的。

我只是给出一个简单的解决方案。

def loop():
while 1:
    print "please input 1,2 or 3"
    num = int(raw_input("> "))
    if num < 1 or num > 3:
        print "please try again"
        continue
    return

loop()

您可以根据需要使用更改。

相关问题