Program keeps looping without calling function

I am writing a program that asks two users boy or girl names and whether they like them or not. This is the beginning of the program and I created a function so if they don't input boy or girl, it asks them again so the program can properly run.

user_name_class= BabyNames()

print "Let's get started!"
print "Who will user1 be and who will user2 be?"

name_user1 = raw_input("User1 = ")
name_user2 = raw_input("User2 = ")

print user_name_class.baby_namer(raw_input("Do you want boy or girl names? \n").lower())

class BabyNames():

def baby_namer(self, gender): 
    self.gender = gender
    if 'b' not in gender or 'g' not in gender:
        return self.start_wrong_input()
    elif 'b' in gender:
        test = raw_input("What round are you on? (1-{}) \n".format(str(len(boy_rounds))))
        return boy(boy_rounds[test])
    elif 'g' in gender:
        test = raw_input("What round are you on? (1-{}) \n".format(str(len(girl_rounds))))
        return girl(girl_rounds[test])
    else:
        pass

def start_wrong_input(self):
    x = BabyNames()
    print "You need to put boy or girl to move on!"

    re_try = raw_input("Do you want boy or girl names? \n").lower()
    if 'g' in re_try:
        return x.baby_namer('girl')
    elif 'b' in re_try:
        return x.baby_namer('boy')      
    else:
        print "You need to put boy or girl to move on!"

I know creating the class probably wasn't necessary, but I just learned about them so I tried incorporating them into my code. What is happening is at "Do you want boy or girl?" if you input anything else besides that, it properly calls the function and re-asks it but then when you put boy or girl, it loops and continues to asks the question without breaking. Why is this happening and let me know if I need to elaborate on anything else!(ps I'm a beginner so it may not be pythonic, but I'm working on it!)

Answers


Use if 'b' not in gender and 'g' not in gender, using or always puts you back to the start_wrong_input method because if gender is girl b is not in girl and if gender is boy g is not in boy so if 'b' not in gender or 'g' not in gender: will always evaluate to True

class BabyNames():
    def baby_namer(self, gender):
        if 'b' not in gender and 'g' not in gender:
            return self.start_wrong_input()
        elif 'b' in gender:
            test = raw_input("What round are you on? (1-{}) \n".format(str(len(boy_rounds))))
            return boy(boy_rounds[test])
        elif 'g' in gender:
            test = raw_input("What round are you on? (1-{}) \n".format(str(len(girl_rounds))))
            return girl(girl_rounds[test])
        else:
            pass

    def start_wrong_input(self):
        print "You need to put boy or girl to move on!"
        re_try = raw_input("Do you want boy or girl names? \n").lower()
        if 'g' in re_try:
            return self.baby_namer('girl') # use self.baby_namer, you don't need to use x = BabyNames()  
        elif 'b' in re_try:
            return self.baby_namer('boy')
        else:
            print "You need to put boy or girl to move on!"

The error in your code is in baby_namer:

if 'b' not in gender or 'g' not in gender:

You probably want a 'and' there, you fail if it is not a boy or if it is not a girl. You could try with entering the 'boyg' value...

Apart from that, you should not use recursive calls to make loops, it's difficult to read ! Use a proper while loop.


You've create something that's called mutual recursion functions

function: baby_namer() calls start_wrong_input() and then start_wrong_input() calls back in return to start_wrong_input(). And you got yourself an infinite loop.


Need Your Help

facebook app doesn't reload after Request for Permission Popup - using PHP SDK (v.3.1.1)

php facebook facebook-php-sdk facebook-apps

My old facebook application requires updating to the latest php sdk as was pointed out

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.