user3675996
user3675996

Reputation:

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

Upvotes: 0

Views: 206

Answers (4)

Borromakot
Borromakot

Reputation: 293

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.

Upvotes: 0

Abdul Fatir
Abdul Fatir

Reputation: 6357

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.

Upvotes: 2

Cristian Garcia
Cristian Garcia

Reputation: 9859

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

Upvotes: 1

Cristian Garcia
Cristian Garcia

Reputation: 9859

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

Upvotes: 1

Related Questions