Home > other >  if statement won't execute all of the commands in its indentation level
if statement won't execute all of the commands in its indentation level

Time:06-10

I am working on a stupid yet funny practice program to improve my understanding of OOP in Python. The program is meant to randomly generate some band names from a randomly selected adjective and another randomly selected noun - producing a lot of hilarious band names.

For the most part, the program works fine, but for some reason, there are some problems with the if-statements and the while loop in the menu(self)- method all the way down in the BandList class.

My hypothesis is that there is something wrong with the nesting of the else-if statements, or that the loop doesn't manage to advance the loop when I call on the self._generateBand() method in line 60 due to some technicality I'm not aware of. Either way, I'm not sure.

However, my question is:

Why does my loop stop at the line self._writeBand() and not continue executing the code that follows? (As shown below)

    done = False
        while done != True:
            print("\n=============== BAND NAME GENEREATOR ==================")

            start = input("\nDo you want to generate a list of bandnames? (y/n): ")
            if start.lower() == "y":
                self._generateBand()
                self._writeBand()    #The loop stops here for some reason and asks the same question over and over.

                #The program won't execute this part of the code.
                inp = ("\nDo you want to save these band names? (y/n): ")
                if inp.lower() == "y":
                    outfile = input("What do you want to name the file?: ")
                    self._saveBand(f"{oufile}.txt")

If anyone can help me fix this, I would be super grateful. In advance: Thank you for your help.

The complete program is pasted in below

import random

class Band:

    def __init__(self, name):
        self._Bandname = name

    def __str__(self):
        return f"{self._Bandname}"

    def hentName(self):
        return self._Bandname

class  BandList:

    def __init__(self):
        self._BandList = []

    def _readFile(self, filename1, filename2):
        with open(filename1) as infile1, open(filename2) as infile2:
            lineAdjective = infile1.read().splitlines()
            lineNoun = infile2.read().splitlines()
            adjective = random.choice(lineAdjective)
            noun = random.choice(lineNoun)

            return f"{adjective} {noun}"

    def _saveBand(self, filename):
        with open(filename, "w") as outfile:
            for j, i in enumerate(self._BandList):
                outfile.write(f"Nr: {j}\t-{i}\n")

    def _generateBand(self):
        num = int(input("\nHow many band names would you like to generate?: "))
        for i in range(num):
            bandname = f"The {self._readFile('adjective.txt', 'noun.txt')}s"
            self._BandList.append(Band(name= bandname))

    def _writeBand(self):
        print("\n========= Genererte bandname =========")
        for i in self._BandList:
            print(i)
            #print(i.hentName())

    def _deleteBand(self):
        self._BandList.clear()

    def _writeGoodbyeMsg(self):
        print("\n============ PROGRAM TERMINATING ================")
        print("\t- thanks for using the program, goodbye!")


    def menu(self):
        done = False
        while done != True:
            print("\n=============== BAND NAME GENEREATOR ==================")

            start = input("\nDo you want to generate a list of bandnames? (y/n): ")
            if start.lower() == "y":
                self._generateBand()
                self._writeBand()           #This is probably where the bug is...
                inp = ("\nDo you want to save these band names? (y/n): ")
                if inp.lower() == "y":
                    utfil = input("What do you want to name the file?: ")
                    self._saveBand(f"{utfil}.txt")

                elif inp.lower() == "n":
                    self._deleteBand()
                    inp2 = input("Do you want to generate more band names? (y/n)?: ")
                    if inp2.lower() == "y":
                        self._generateBand()
                    elif inp2.lower() == "n":
                        done = True
                        self._writeGoodbyeMsg()
                    else:
                        print("Unknown command, please try again")

            else:
                self._writeGoodbyeMsg()
                done = True

if __name__ == '__main__':

    new =  BandList()
    new.menu()

CodePudding user response:

You're missing an input call on your 2nd question for saving the band names. It should be:

 inp = input("\nDo you want to save these band names? (y/n): ")

CodePudding user response:

It does work. You just haven't given any values in self._BandList.

Its returning "BandList": null.

  • Related