Home > Blockchain >  Need help fixing a ValueError of and 'i' index
Need help fixing a ValueError of and 'i' index

Time:04-19

So I'm making this code that uses strings to cipher a given phrase with a given key (I can't use the preset functions in python to do it). I believe I've got the idea of how to do it but I;m experiencing this error that I can't figure out how to fix. My code is the following.

def inputForChipher():
    input1 = False
    input2 = False
    while input1 == False:
        string = input('Enter the message: ')
        input1 = verifyString(string)
    while input2 == False:
        key = input('Enter the key you want to use: ')
        input2 = verifyString(key)
    print(cipherWithKey(string, key))

def cipherWithKey(string, key):
    string = string.lower()
    key = key.lower()
    letters = 'abcdefghijklmnopqrstuvwxyz'
    count = 0
    newCode = ''
    for i in string:
        if i == '':
            count = 0
            newCode  = i
        else:
            if count > (len(key)-1):
                count = 0
            value1 = letters.index(i)   1
            value2 = letters.index(key[count])   1
            num = value1   value2
            if num > 26:
                num -= 26
            newCode  = letters[num-1]
            count  = 1
    return newCode

And I'm getting the following error:

File "C:\Users\xxxxx\Desktop\funciones.py", line 29, in cipherWithKey
    value1 = letters.index(i)   1
ValueError: substring not found

I know what causes this error, but I'm not sure of why I'm getting it in this case.

CodePudding user response:

well its clear , in your input 'tarea de' you are passing space in your input ( string varible) which is not in letters variable and it fails in that line.

you can add the possible characters to your letters variable like :

letters = 'abcdefghijklmnopqrstuvwxyz '

alternatively you can use string module to provide the full list of whitespaces

import string as st 
letters = st.ascii_lowercase   st.whitespace
  • Related