Python Forum
local variable 'option' referenced before assignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
local variable 'option' referenced before assignment
#1
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:
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. 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 16, in heavyMetalCDMixName while option != 100: UnboundLocalError: local variable 'option' referenced before assignment
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?
Reply


Messages In This Thread
local variable 'option' referenced before assignment - by RedSkeleton007 - Jan-22-2018, 06:07 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  It's saying my global variable is a local variable Radical 5 1,246 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  local varible referenced before assignment SC4R 6 1,584 Jan-10-2023, 10:58 PM
Last Post: snippsat
  UnboundLocalError: local variable 'wmi' referenced before assignment ilknurg 2 1,955 Feb-10-2022, 07:36 PM
Last Post: deanhystad
  Referenced before assignment finndude 3 3,323 Mar-02-2021, 08:11 PM
Last Post: finndude
  ReferenceError: weakly-referenced object no longer exists MrBitPythoner 17 11,736 Dec-14-2020, 07:34 PM
Last Post: buran
  Assignment of non-existing class variable is accepted - Why? DrZ 6 4,340 Jul-13-2020, 03:53 PM
Last Post: deanhystad
  UnboundLocalError: local variable 'figure_perso' referenced before assignment mederic39 2 2,300 Jun-11-2020, 12:45 PM
Last Post: Yoriz
  local variable 'marks' referenced before assignment Calli 3 2,363 May-25-2020, 03:15 PM
Last Post: Calli
  Variable assignment wierdness with Memory eoins 1 2,068 Mar-08-2020, 10:15 AM
Last Post: scidam
  UnboundLocalError: local variable referenced before assignment svr 1 3,342 Dec-27-2019, 09:08 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020