1

I can not work out why it keeps repeating the function, even though the condition to change the while loop to false is met. Here is an example of what I am trying to do:

confirm=True

def program():
    name=input("What is your name? ")
    country=input("What is your country? ")

    runagain=input("Would you like to run again? Enter no or yes: ")
    if runagain=="no":
        print("Thank You")
        confirm=False
    else:
        print("Rerun")
        confirm=True

while confirm==True:
    program()
Chris_Rands
  • 38,994
  • 14
  • 83
  • 119
  • 1
    `confirm` in the global scope is different from `confirm` in the function. You never change the global variable `confirm`. Anyway, look at this http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – Chris_Rands May 08 '17 at 10:36

3 Answers3

3

You have to use global confirm in your method program.

Check this out

confirm=True

def program():
    global confirm
    name=input("What is your name? ")
    country=input("What is your country? ")

    runagain=input("Would you like to run again? Enter no or yes: ")
    if runagain=="no":
        print("Thank You")
        confirm=False
    else:
        print("Rerun")
        confirm=True

while confirm:
    program()

Using global is a bad sign of writing code. Instead you remove a lot of code in your code, making it fewer lines. Refer this

def program():
    name=input("What is your name? ")
    country=input("What is your country? ")
    runagain=input("Would you like to run again? Enter no or yes: ")
    if runagain=="no":
        print("Thank You")
        exit()
    else:
        print("Rerun")

while 1:
    program()
bigbounty
  • 16,526
  • 5
  • 37
  • 65
  • 1
    `exit()` inside `program()` might solve the dummy example, but it is not robust. What if he wants to do something else after that? It will be much better if `program()` will just return if it succeeded or not. – Liran Funaro May 08 '17 at 10:56
  • I wrote this answer particularly for his code to work.I understand your point – bigbounty May 08 '17 at 10:57
2

Python is Statically Scoped

http://python-textbok.readthedocs.io/en/1.0/Variables_and_Scope.html

# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7 as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements will give us name errors!
print(c)
print(d)

The global keyword works but may be counterproductive...

Note that it is usually very bad practice to access global variables from inside functions, and even worse practice to modify them. This makes it difficult to arrange our program into logically encapsulated parts which do not affect each other in unexpected ways. If a function needs to access some external value, we should pass the value into the function as a parameter. If the function is a method of an object, it is sometimes appropriate to make the value an attribute of the same object

Kickaha
  • 3,680
  • 6
  • 38
  • 57
0

Using global should be avoided if possible. Although using global will solve it, it would be wiser to avoid it.

If program() would have returned if it succeeds, that would have been helpful.

For example:

def program():
    name=input("What is your name? ")
    country=input("What is your country? ")

    runagain=input("Would you like to run again? Enter no or yes: ")
    if runagain=="no":
        print("Thank You")
        return False
    else:
        print("Rerun")
        return True

while program():
    pass
Liran Funaro
  • 2,750
  • 2
  • 22
  • 33