0

The code below keeps skipping the "TryAgainQuestion()" function:::: I Realy need some help!!! It is becoming annoying

    import time
    ranname = "0"

def AskForName():
    ranname = "1"
    name = input("Please enter your name: ")
    print("--------------------")
    print("  .:: ",name," ::.")
    print("--------------------")
    print("     ")
    time.sleep(2)
    Start()

def TryAgainQuestion():
    tryagain = input("Do you want to try again? (Y or N): ")
    if (tryagain == "y" or "Y"):
        AskForName()

    else:
        time.sleep(1)
        print("Made By: Daniel Parker")

def Start():
    if (ranname == "1"):
        TryAgainQuestion()

    if (ranname == "0"):
        AskForName()
Start()

Thanks, Dan

DParker
  • 3
  • 3
  • 1
    You can't change `ranname` in `AskForName`; it's instead treated as a local variable of `AskForName`. See this question: http://stackoverflow.com/questions/10588317/python-function-global-variables – Waleed Khan Oct 14 '13 at 12:13
  • 3
    If your tutors don't know what's wrong here you might want to consider finding a better educational institution. – aukaost Oct 14 '13 at 12:13
  • 1
    Also, `tryagain == "y" or "Y"` probably doesn't do what you want it to do. Try `tryagain in ["y", "Y"]`. – Waleed Khan Oct 14 '13 at 12:14
  • 1
    And the school tutors didn't mention the unnecessary use of parentheses or the very non-Pythonic naming convention either? Have a look at: http://stackoverflow.com/questions/19355810/if-statement-checking-for-a-string-in-a-list-behave-weirdly – Jon Clements Oct 14 '13 at 12:15
  • Learnign python, I think you have a serious problem. I mean, your school tutors certainly does. – aIKid Oct 14 '13 at 12:49
  • @aIKid, yes, the OP has a serious problem - his/her school -_- – Wayne Werner Oct 15 '13 at 13:41

2 Answers2

2

It's a scope problem : the ranmame at the second line is not the same as the one in the AskForName function. You need to add global ranname at the beginning of the function to update properly.

import time
ranname = "0"

def AskForName():
    global ranname
    ranname = "1"
lucasg
  • 10,734
  • 4
  • 35
  • 57
0

As suggested and said by @georgesl it's scope problem (namespace)

Please read this guide, i'm attaching a working code by a class:

import time

class myClass():

    def __init__(self):
        self.ranname = "0"

    def AskForName(self):
        self.ranname = "1"
        name = raw_input("Please enter your name: ")
        print("--------------------")
        print("  .:: ", name, " ::.")
        print("--------------------")
        print("     ")
        time.sleep(2)
        self.Start()

    def TryAgainQuestion(self):
        tryagain = raw_input("Do you want to try again? (Y or N): ")
        if (tryagain == "y"):
            self.AskForName()

        else:
            time.sleep(1)
            print("Made By: Daniel Parker")

    def Start(self):
        if self.ranname == "1":
            self.TryAgainQuestion()

        if self.ranname == "0":
            self.AskForName()

def main():

    myclass = myClass()
    myclass.Start()

   #end of main

if __name__ == "__main__":
    main()
Kobi K
  • 7,743
  • 6
  • 42
  • 86