简体   繁体   English

在我的代码中获取无法将int转换为str错误无法弄清原因

[英]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. 在这种情况下, vowelToFind是一个整数,而vowels是一个字符串。 The find method of str accepts a string argument, and returns its position in the string - you are passing an integer instead. strfind方法接受一个字符串参数,并返回其在字符串中的位置-您正在传递一个整数 You aren't even looking to find a substring within vowels , you are simply looking to retrieve the character at index vowelToFind . 您甚至都没有在寻找vowels内的子字符串,只是在索引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. 我正在解决您询问的特定“ int to str”错误。 I'm not sure about the rest of the logic you're using to generate an "easy-to-remember" character sequence. 我不确定您用于生成“易于记忆”的字符序列的其余逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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