ValueError: need more than 1 value to unpack

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thestudent
    New Member
    • Mar 2010
    • 1

    ValueError: need more than 1 value to unpack

    So I am trying to write a program to use a cypher to code a textfile. There is a textfile holding the cypher, and the program is supposed to ask the user which file they want encoded. This is my program so far
    Code:
    #program
    textfile = input("What is the name of the file that holds a cypher?")
    code = {}
    for line in textfile:
            (char, shadow_char) = line.split()
            code[char]= shadow_char
    infile = input("What is the name of the file that holds a text?")
    outfile = input("Choose a name of a file where the encoded text should be printed?")
    
    INFILE = open(infile, "r")
    OUTFILE = open(outfile, "w")
    for line in INFILE:
        for char in sentence:
            if char in code:
                encoded_sentence += code[char]
            else:
                encoded_sentence += char
    
        OUTFILE.write(sentence+ "\n")
        OUTFILE.write(econded_sentence)
       
    INFILE.close()
    OUTFILE.close()


    i keep getting "ValueError : need more than 1 value to unpack"
    for line 4, (char, shadow_char) = line.split()

    any help?
    Last edited by bvdet; Mar 14 '10, 02:52 PM. Reason: Add code tags
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    line.split() will just return one value. Your code is trying to assign two variables to the result, so Python is telling you that it does not love this.

    Try myChar=line.spl it(), and then myChar[0] for char and myChar[1] for shadow_char.

    Comment

    Working...