2014-05-18 243 views
0

是的,我是新来的,我很难学习。这让我非常困惑。我尝试了几个小时来创建一个循环,如果变量的值不是它应该的值,它会继续询问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" 

回答

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()