简体   繁体   English

在Python中循环使用

[英]While-Loop Use in Python

When you come to the 2nd while loop while x == 2: it's still repeating the whole script even though x /= 1 (not if "n" is entered). 当您while x == 2:进入第二while循环while x == 2:即使x /= 1它仍在重复整个脚本(如果输入"n"则不是)。 Let say we enter "y" on the prompt "Is this correct?" 假设我们在提示"Is this correct?"上输入"y" shouldn't x become 3 which stops both the first and the 2nd loop? x难道不应该成为3从而使第一个循环和第二个循环都停止吗?

This is probably obvious but I'm pretty new. 这可能很明显,但是我很新。

 # -*- 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 )

In the code you never change the value of your x to 2 so your inner loop while x==2: never runs and you loop infinitely. 在代码中,您永远不会将x的值更改为2,因此while x==2: ,您的内部循环将永远不会运行,并且会无限循环。 You need to change the value of x just inside the while x==1: loop for you to even enter the second loop. 您需要在while x==1:循环内更改x的值,以使您甚至进入第二个循环。

The while structure is totally unnecessary, use functions instead and chain them 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

While I like my other answer better, if you want this code to work with just a slight modification, just bring the definition of correct to the inner loop and as Abdul Fatir say, kick in an x = 2. Anyhow using creating a state machine this way is not recommended. 虽然我更喜欢我的其他答案,但是如果您希望此代码仅需稍加修改就可以工作,则只需将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

I like the solution involving chaining functions, but I also think that you could use some help with your input validation. 我喜欢涉及链接功能的解决方案,但我也认为您可以在输入验证中使用一些帮助。 I really nice tool for this is not in to validate it ahead of time. 我真的很好用的工具not in提前验证它。 For instance 例如

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

Just to be clear, I ripped a large portion of that code from another answer, but I think that doing it that way is more efficient, and puts all acceptable answers in once place for easy viewing. 为了清楚起见,我从另一个答案中删除了大部分代码,但是我认为这样做更有效,并将所有可接受的答案放在一个地方以便于查看。 It would even allow for more complex input validation, and potentially let you use a constant or config file to define available options. 它甚至可以进行更复杂的输入验证,并有可能让您使用常量或配置文件来定义可用选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM