Jan-22-2018, 07:52 AM
My VWL list is out of scope:
#!/usr/bin/env python3 #BuildYourOwnCDmixName.py def heavyMetalCDMixName(): yourName = input("What is your name: ") VWL = ["Angry", "Violent", "Crazy", "Dangerous", "Berserk"] #VWL = ViolentWordList print("The words in the list include: " + "[0]:" + VWL[0] + ", [1]:" + VWL[1] + ", [2]:" + VWL[2] + ", [3]:" + VWL[3] + ", [4]:" + VWL[4]) print("Choose index numbers from [0] to [4] to complete the name for your " + str(yourName) + "'s _______ Heavy Metal mix. Enter 100 when you're done.") mixName = (str(yourName) + "'s ") option = True while True: option = int(input("Enter index number to append to your CD mix's name: ")) if option == 0: mixNameList = list(mixName)#convert mixName to a list, mixNameList.insert(0, VML[0])#so we can add the word at VML[0] to the cd mix name mixName = ''.join(mixNameList)#convert mixName back to a string elif option == 1: mixNameList = list(mixName) mixNameList.insert(0, VML[1]) mixName = ''.join(mixNameList) elif option == 2: mixNameList = list(mixName) mixNameList.insert(0, VML[2]) mixName = ''.join(mixNameList) elif option == 3: mixNameList = list(mixName) mixNameList.insert(0, VML[3]) mixName = ''.join(mixNameList) elif option == 4: mixNameList = list(mixName) mixNameList.insert(0, VML[4]) mixName = ''.join(mixNameList) elif option == 100: print("You named your CD mix: " + mixName) break #so is "break" equivalent to "option = false"? else: print("You must enter a valid index number between 0 and 4 from VML.") continue def main(): heavyMetalCDMixName() main()
Error:What is your name: Sam
The words in the list include: [0]:Angry, [1]:Violent, [2]:Crazy, [3]:Dangerous, [4]:Berserk
Choose index numbers from [0] to [4] to complete the name for your Sam's _______ Heavy Metal mix. Enter 100 when you're done.
Enter index number to append to your CD mix's name: 0
Traceback (most recent call last):
File "I:\Python\Python36-32\SamsPrograms\BuildYourOwnCDmixName.py", line 48, in <module>
main()
File "I:\Python\Python36-32\SamsPrograms\BuildYourOwnCDmixName.py", line 46, in main
heavyMetalCDMixName()
File "I:\Python\Python36-32\SamsPrograms\BuildYourOwnCDmixName.py", line 20, in heavyMetalCDMixName
mixNameList.insert(0, VML[0])#so we can add the word at VML[0] to the cd mix name
NameError: name 'VML' is not defined
Do I really have to put it inside the while loop? Is there another way to deal with that scope problem?