0

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). Let say we enter "y" on the prompt "Is this correct?" shouldn't x become 3 which stops both the first and the 2nd loop?

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 )
1
  • 2
    I don't see anything changing x to 2, so your inner while never runs Commented May 29, 2014 at 18:37

4 Answers 4

2

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. You need to change the value of x just inside the while x==1: loop for you to even enter the second loop.

Sign up to request clarification or add additional context in comments.

4 Comments

Unless there was an edit, x changes here else: print "Invalid input \n\t Loading..." x = 2 # x = 2 --> 2nd while takes over
@CristianGarcia If you just went over zealous and down voted my answer because of this then I advice you to read the code before doing such a thing. The line x = 2 # x = 2 --> 2nd while takes over is inside an else which is inside a while with condition while x==2: So you never enter the loop and never change x to 2.
Sorry about that, at first I thought you aswer was wrong because I did see the x = 2 down there, but then I realized that the problem was elsewhere. Will up vote in 10min when the system unlocks me.
Abdul, please make a slight modification to the answer so the systems unlocks me.
1

The while structure is totally unnecessary, use functions instead and chain them

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

Comments

1

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.

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

1 Comment

This question should be an edit in your other question.
0

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. 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.

1 Comment

It was proposed that correct is already "y" or "n". But it would not be the case. This is input validation. Essentially, the user may have entered 1. Or True. Correct is not already in ["y", "n"]. Also I think there is some confusion. I was using 'not in'. Essentially I said if correct is not "y" or if correct is not "n", ask them to define correct again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.