简体   繁体   中英

Getting can't convert int to str error in my code can't figure out why

My code is supposed to take a pin number and change it into an "easy to remember" arrangement of vowels and consonants

def alphapinEncode(pin):
    vowels = "aeiou "
    consonants = "bcdfghjklmnpqrstvwyz "
    alphaPin = " "
    while pin > 0:
        newPin = pin // 100
        pinRemain = pin % 100
        vowelToFind = pinRemain % 5
        pinVowel = vowels.find(vowelToFind)
        pinConsonant = consonants.find(pinRemain // 5)
        pin = newPin
        alphaPin = alphaPin + pinVowel + pinConsonant

    return alphaPin

Any help is appreciated, thanks!

I believe your issue is with these 2 lines:

vowelToFind = pinRemain % 5
pinVowel = vowels.find(vowelToFind)

In this case vowelToFind is an integer, and vowels is a string. The find method of str accepts a string argument, and returns its position in the string - you are passing an integer instead. You aren't even looking to find a substring within vowels , you are simply looking to retrieve the character at index vowelToFind . I believe you ought to do the following instead:

vowelToFind = pinRemain % 5
pinVowel = vowels[vowelToFind]

Note

I'm solving the specific "int to str" error that you asked about. I'm not sure about the rest of the logic you're using to generate an "easy-to-remember" character sequence.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM