我似乎无法理解循环

时间:2014-05-18 00:48:13

标签: loops python-2.7

是的我是新手,我很难学习。这让我非常困惑。我已经尝试了几个小时来创建一个循环,如果变量的值不是它应该是什么,它将继续询问raw_input,直到它应该是什么,程序移动到下一个函数,以及显示一些东西喜欢“再试一次”它可能很简单,但我无法弄明白。我在谈论first_action = raw_input(“>>”)之后的代码

def start():
    print """You awaken in a strange lanscape, You are on a steep hillside 
    surrounded by trees, and you can see large mountains in the distance."""
    print "....."
    print "....."
    print "You struggle to regain your senses, and you realize you don't know who you are, or how you got here."
    print "You have the following items: %s" % i
    print "It's dark, perhaps you should sleep until the sun rises."
    print "What do you do?"

def cryptic_message(read):
    print read, "A paper with a message written in a strange language."

def day_1():
    print "_-_-_Dream_-_-_"
    print "It is relativity. It is a concept you know as time."
    print """This time, what is it? You ask "me" and I do not know. Ask yourself."""
    print """It is illusion. Oh look, "time" to wake up!"""
    print "....."
    print "....."
    print "You awaken, dazed and tired"
    print "What do you do?"

def knife(cut, stab):
    print "What do you want to do with the knife?"
    knife = raw_input(">> ")

def photo(view):
    print "A faded photo of a man on a park bench"

i = ['clothing','small knife','strange photo','cryptic message']

start()
first_action = raw_input(">> ")
first_action == False
while first_action is False:
    if first_action == "sleep":
        print "You lay on the grass for a hardly restful sleep."
        first_action == True
        day_1()
    elif first_action == "do nothing":
        print "Try again"
        first_action == False
    else: "Try again"

1 个答案:

答案 0 :(得分:0)

你想要这样的东西:

inp = ''
desired = 'hello'
while inp != desired:
    inp = raw_input('Enter a greeting: ')

inpdesired之前,这将继续要求输入:

>>> inp = ''
>>> desired = 'hello'
>>> while inp != desired:
...     inp = raw_input('Enter a greeting: ')
... 
Enter a greeting: bye
Enter a greeting: no
Enter a greeting: what's up
Enter a greeting: hello
>>> 

在您的代码中:

first_action = raw_input(">> ")

while first_action != "False::
    print "Try again"
    first_action = raw_input(">> ")
print "You lay on the grass for a hardly restful sleep."
day_1()