![]() |
local variable 'option' referenced before assignment - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: local variable 'option' referenced before assignment (/thread-7717.html) Pages:
1
2
|
local variable 'option' referenced before assignment - RedSkeleton007 - Jan-22-2018 As an example of what my program is supposed to do, lets say, for example, that the user's name was John, and he chose option 0, and then chose option 100. The resulting cd mix name should be "John's Angry Heavy Metal Mix". #!/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 = VWL[]#declare option, so we can use it inside while loop while option != 100: 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 else: print("You must enter a valid index number between 0 and 4 from VML.") continue def main(): heavyMetalCDMixName() main()The error is this: I understand what the error is, but I don't know how to declare it (so I can use it) without having to initialize it.How do I fix this problem? RE: local variable 'option' referenced before assignment - Gribouillis - Jan-22-2018 You cannot evaluate the boolean expression option != 100 if option does not already have a value. You could as well replace the while option != 100 by while True .
RE: local variable 'option' referenced before assignment - RedSkeleton007 - Jan-22-2018 (Jan-22-2018, 06:36 AM)Gribouillis Wrote: You cannot evaluate the boolean expressionIt still doesn't work: #!/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() Also, I never understood the code statement "while true". While what is true?
RE: local variable 'option' referenced before assignment - Gribouillis - Jan-22-2018 (Jan-22-2018, 07:01 AM)RedSkeleton007 Wrote: I never understood the code statement "while true". While what is true?In python, true is written True with a capital T. The while True idiom means loop indefinitely. It may seem strange but it's simple logic: a while condition: loop runs until the condition becomes false. The True condition never becomes false, so while True loops indefinitely, without the need to add a new keyword such as repeat in the language.C programmers traditionally write while(1) to loop indefinitely. This is the ancestor of python's while True . The latter is cleaner from a purist's point of view because a condition is a boolean rather than an integer. However even python uses 1 for True and 0 for False, for example>>> True + 1 2but >>> True is 1 False RE: local variable 'option' referenced before assignment - RedSkeleton007 - Jan-22-2018 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() Do I really have to put it inside the while loop? Is there another way to deal with that scope problem?
RE: local variable 'option' referenced before assignment - j.crater - Jan-22-2018 Problem is that you first defined VWL, not VML. RE: local variable 'option' referenced before assignment - Gribouillis - Jan-22-2018 (Jan-22-2018, 07:52 AM)RedSkeleton007 Wrote: My VWL list is out of scope:How does VWL become VML ? RE: local variable 'option' referenced before assignment - RedSkeleton007 - Jan-22-2018 Nothing shows up for the mixName string: #!/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.") name = (str(yourName) + "'s ") option = True while True: mixNameList = [] mixName = "" option = int(input("Enter index number to append to your CD mix's name: ")) if option == 0: mixNameList = list(mixName) mixNameList.insert(0, VWL[0]) mixName = ''.join(mixNameList) elif option == 1: mixNameList = list(mixName) mixNameList.insert(0, VWL[1]) mixName = ''.join(mixNameList) elif option == 2: mixNameList = list(mixName) mixNameList.insert(0, VWL[2]) mixName = ''.join(mixNameList) elif option == 3: mixNameList = list(mixName) mixNameList.insert(0, VWL[3]) mixName = ''.join(mixNameList) elif option == 4: mixNameList = list(mixName) mixNameList.insert(0, VWL[4]) mixName = ''.join(mixNameList) elif option == 100: print("You named your CD mix: " + name + " " + mixName + "Heavy Metal Mix") break else: print("You must enter a valid index number between 0 and 4 from VML.") continue def main(): heavyMetalCDMixName() main() The result should be "Sam's AngryViolent Heavy Metal Mix", not "Sam's Heavy Metal Mix".What is going on? RE: local variable 'option' referenced before assignment - j.crater - Jan-22-2018 Quote:mixNameList = [] You put this in the beginning / inside of while loop. Each loop iteration, you "reset" these two variables. Pay attention to variables scope. RE: local variable 'option' referenced before assignment - RedSkeleton007 - Jan-22-2018 Mission accomplished! Code works correctly: #!/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.") name = (str(yourName) + "'s ") mixNameList = [] mixName = "" option = True while True: option = int(input("Enter index number to append to your CD mix's name: ")) if option == 0: mixNameList = list(mixName) mixNameList.insert(0, VWL[0]) mixName = ''.join(mixNameList) elif option == 1: mixNameList = list(mixName) mixNameList.insert(0, VWL[1]) mixName = ''.join(mixNameList) elif option == 2: mixNameList = list(mixName) mixNameList.insert(0, VWL[2]) mixName = ''.join(mixNameList) elif option == 3: mixNameList = list(mixName) mixNameList.insert(0, VWL[3]) mixName = ''.join(mixNameList) elif option == 4: mixNameList = list(mixName) mixNameList.insert(0, VWL[4]) mixName = ''.join(mixNameList) elif option == 100: print("You named your CD mix: " + name + mixName + " Heavy Metal Mix") break else: print("You must enter a valid index number between 0 and 4 from VML.") continue def main(): heavyMetalCDMixName() main()Admins, you can now close this thread. Thanks for your help everyone ;) |