我正在開發一個愚蠢而有趣的練習程式,以提高我對 Python 中 OOP 的理解。該程式旨在從隨機選擇的形容詞和另一個隨機選擇的名詞中隨機生成一些樂隊名稱 - 產生很多熱鬧的樂隊名稱。
在大多數情況下,程式運行良好,但由于某種原因,if 陳述句和menu(self)- 方法中的 while 回圈一直存在問題BandList class。
我的假設是 else-if 陳述句的嵌套有問題,或者當我呼叫self._generateBand() 第 60 行中的方法時,由于某些我不知道的技術性,回圈無法推進回圈。無論哪種方式,我都不確定。
但是,我的問題是:
為什么我的回圈停在該行self._writeBand()而不繼續執行后面的代碼?(如下所示)
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")
如果有人能幫我解決這個問題,我將非常感激。提前:感謝您的幫助。
完整的程式粘貼在下面
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()
uj5u.com熱心網友回復:
您錯過了input關于保存樂隊名稱的第二個問題的電話。它應該是:
inp = input("\nDo you want to save these band names? (y/n): ")
uj5u.com熱心網友回復:
它確實有效。您只是沒有在self._BandList.
它的回歸"BandList": null。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/488608.html
上一篇:有沒有辦法用用戶輸出創建一個包含“插入”和“洗掉”的程序?
下一篇:陣列中的第二大唯一元素
