Python Forum
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


RE: local variable 'option' referenced before assignment - buran - Jan-22-2018

well, now when it works, you may want to refactor your code in order to improve it.
e.g. your while loop, because it is repetitive code that screams for change, but it's not only that...
just a challenge for you :-)


RE: local variable 'option' referenced before assignment - RedSkeleton007 - Feb-23-2018

Actually, the string still doesn't print 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()
But the output of my string is backwards. For example:
Output:
=== RESTART: I:\Python\Python36-32\SamsFavorites\BuildYourOwnCDmixName.py === 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 Enter index number to append to your CD mix's name: 1 Enter index number to append to your CD mix's name: 3 Enter index number to append to your CD mix's name: 100 You named your CD mix: Sam's DangerousViolentAngry Heavy Metal Mix >>>
I was expecting "Sam's AngryViolentDangerous Heavy Metal Mix", because that's the order I originally selected them from the VWL list.
What happened?