在Python中循环使用

时间:2014-05-29 18:36:07

标签: python loops while-loop nested-loops

当你来到第二个while循环while x == 2:时,它仍在重复整个脚本,即使x /= 1(不是输入"n")。假设我们在提示符"y"上输入"Is this correct?"不应该x变为3,这会停止第一个和第二个循环吗?

这可能很明显,但我很新。

 # -*- coding: utf-8 -*-
import time

x = 1
while x == 1:
    print "What's your name?"
    name = raw_input("Name: ")
    print "How old are you?"
    age = raw_input("Age: ") 
    print "So you are %r years old and your name is %r. Is this correct?" % (age, name)
    correct = raw_input("(y/n): ")

    while x == 2:
        if correct == "y":
            x = 3          # x = 3 skips all whiles, not working
            time.sleep(1)

        elif correct == "n":    
            time.sleep(1) 
            x = 1         # x = 1 --> 1st while still in action

        else:
            print "Invalid input \n\t Loading..."   
            x = 2         # x = 2 --> 2nd while takes over

print "Where do you live?" 
time.sleep(0.5)
country = raw_input("Country: ")
time.sleep(0.5)
city = raw_input("City: ")
time.sleep(1)

print " \n Data: \n\t Name: %r \n \t Age: %r \n \t Country: %r \n \t 
City: %r " % (name, age, country, city )

4 个答案:

答案 0 :(得分:2)

在代码中,您永远不会将x的值更改为2,因此您的内循环while x==2:永远不会运行,并且您无限循环。您需要在x循环内更改while x==1:的值,以便进入第二个循环。

答案 1 :(得分:1)

while结构完全没必要,使用函数代替并链接它们

def ask():
    print "What's your name?"
    name = raw_input("Name: ")
    print "How old are you?"
    age = raw_input("Age: ")

    return decide(name,age) #<-- Goes to decide

def decide(name,age):
    print "So you are %r years old and your name is %r. Is this correct?" % (age, name)
    correct = raw_input("(y/n): ")

    if correct == "y":
        return name,age #<-- process finishes

    elif correct == "n":    
        return ask() #<-- goes back to asking

    else:
        print "Invalid input"
        return decide(name,age) #<--Goes back to wait for a valid input

name, age = ask() #<--Starts the whole process

答案 2 :(得分:1)

虽然我更喜欢我的其他答案,但是如果你想让这段代码只需要稍作修改就行,只需将correct的定义带到内循环中,就像Abdul Fatir所说的那样,踢x = 2无论如何都不建议以这种方式创建state machine

x = 1
while x == 1:
    print "What's your name?"
    name = raw_input("Name: ")
    print "How old are you?"
    age = raw_input("Age: ") 

    x = 2 #<--Necessary

    while x == 2:
        print "So you are %r years old and your name is %r. Is this correct?" % (age, name)
        correct = raw_input("(y/n): ")

        if correct == "y":
            x = 3          # x = 3 skips all whiles, not working
            time.sleep(1)

        elif correct == "n":    
            time.sleep(1) 
            x = 1         # x = 1 --> 1st while still in action

        else:
            print "Invalid input \n\t Loading..."   
            x = 2         # x = 2 --> 2nd while takes over

答案 3 :(得分:0)

我喜欢涉及链接功能的解决方案,但我也认为您可以在输入验证中使用一些帮助。我真的很好的工具是not in提前验证它。例如

def ask():
    print "What's your name?"
    name = raw_input("Name: ")
    print "How old are you?"
    age = raw_input("Age: ")

    return decide(name,age) #<-- Goes to decide

def decide(name,age):
    print "So you are %r years old and your name is %r. Is this correct?" % (age, name)
    correct = raw_input("(y/n): ")
    while correct not in ["y", "n"]:
        correct = raw_input("(y/n): ")
    if correct == "y":
        return (name,age) #<--process finishes
    else  
        return ask() #<-- goes back to asking

为了清楚起见,我从另一个答案中删除了大部分代码,但我认为这样做更有效率,并将所有可接受的答案放在一个地方以便于查看。它甚至可以允许更复杂的输入验证,并且可能允许您使用常量或配置文件来定义可用选项。