【问题标题】:Python: Letters to numbers not working correctlyPython:数字字母无法正常工作
【发布时间】:2017-06-24 04:25:14
【问题描述】:

所以我有这个学校项目,我非常接近完成它,但有一个我似乎无法正常工作。我拥有的功能之一是:

vowels = "aeiou"
consonants = "bcdfghjklmnpqrstvwyz"

def alphapinDecode(tone):
   s = tone.lower()
   pin = ''

   for ch in s:
       if ch in consonants:
           idx = consonants.find(ch)
       elif ch in vowels:
           idx2 = vowels.find(ch)

           pin = str(pin) + str(idx*5 + idx2)




   print(pin)

   return None

在大多数情况下,该功能完全按照我的意愿运行。我取一个字符串,它以字符串形式返回数字。

例如:

>>> alphapinDecode('bomelela')
3464140

但是当我这样做时:

>>>> alphapinDecode('bomeluco')

它返回 346448 而不是它应该做的 3464408 (根据我的任务)。现在我知道该函数根据代码给了我正确的答案,但是我缺少什么让它在 8 之前包含 0?

编辑:

函数应该把你传递的字符串(音调)分解成 2 个字母块(元音/辅音对)。对于这对,它应该使用这对并用元音/辅音索引它们并返回一个数字。 >>>alphapinDecode('hi') 返回 27,因为 consonants[h] 给出 idx = 5 而 vowels[i] 给出 idx2 = 2

【问题讨论】:

  • 如果你能解释一下你的函数到底应该做什么以及如何做会有所帮助。
  • 对不起,我以为我做到了......但它应该把你传递的字符串(音调)分解成 2 个字母块(元音/辅音对)。对于这对,它应该使用这对并用元音/辅音索引它们并返回一个数字。 >>>alphapinDecode('hi') 返回 27,因为 consonants[h] 给出 idx = 5 而 vowels[i] 给出 idx2 = 2
  • 请更新您的问题,以便其他用户知道要回答什么。
  • 你确定 pin = str(pin) + str(idx*5 + idx2) 的缩进是正确的吗?你想用 idx*5 做什么,你能解释一下吗?
  • idx*5 只是我的班级在项目中使用的公式。关于缩进,我刚才在上面解释过了。

标签: python string function return


【解决方案1】:

我认为您的讲座试图测试学生的编码适应性。 如果真的想实现这样的输出,请尝试如下

vowels = "aeiou"
consonants = "bcdfghjklmnpqrstvwyz"

def alphapinDecode(tone):
    s = tone.lower()
    pin = ''

for ch in s:
   if ch in consonants:
       idx = consonants.find(ch)
   elif ch in vowels:
       idx2 = vowels.find(ch)

       num = '%02d' % int((idx*5) + idx2) #python 2
       num = "{0:0=2d}".format((idx*5) + idx2) #python 3 more verbose
       pin = pin + str(num)

   print(int(pin))

   return None

alphapinDecode('bomeluco') # 3464408

alphapinDecode('bomelela') # 3464140

【讨论】:

  • 什么是 '%02d' %?
  • @user2951723 '%02d' 将整数 'd' 格式化为最小宽度为 2 的字段,左侧(前导)用零填充,当与左侧的字符串一起使用时,第二个 '%' 运算符称为 string formatting 运算符。
【解决方案2】:

您的方法可能很尴尬 - 我会一次迭代两个字符:

def alphapinDecode(tone):
   s = tone.lower()
   pin = ''

   # Step over the string two characters at a time
   for i in range(0, len(s), 2):
       ch1 = s[i]
       ch2 = s[i+1]
       if ch1 in consonants and ch2 in vowels:
           idx1 = consonants.find(ch1)
           idx2 = vowels.find(ch2)
           this_pair = idx1*5 + idx2
           # For debugging
           print(this_pair)
           pin = pin + str(this_pair)

   # We need to print without leading zeroes
   print(int(pin))

   # Returning the pin as an integer is better, IMO
   return int(pin)

好的,现在我们的代码看起来更好了,我希望我们可以看到,对于第二个文本中的 co 对,值是 1*5 + 3,等于 8,当然,但你真的想要08。有很多方法可以做到这一点,但由于您是初学者,我将举例说明最简单的方法。

this_pair = idx1*5 + idx2
if this_pair < 10:
    # If the pair is less than ten, prepend a leading zero
    this_pair_pin = '0' + str(this_pair)
else
    this_pair_pin = str(this_pair)
pin = pin + this_pair_pin

编辑:让我们忘记在字符串中累积答案,因为我们可以简化代码:

pin = 0
#...
        this_pair = idx1*5 + idx2
        pin = pin * 100 + this_pair

print(pin)

【讨论】:

  • 这不会在 3 前面添加一个 0 吗?所以它最终是 03464408
  • @user2951723 啊,你是对的,我删除的评论是错误的......
猜你喜欢
  • 1970-01-01
  • 2014-05-14
  • 1970-01-01
  • 2016-08-12
  • 2015-10-12
  • 1970-01-01
  • 2016-10-25
  • 2023-03-28
  • 1970-01-01
相关资源
最近更新 更多