【问题标题】:Detecting characters and acting on them检测角色并对其采取行动
【发布时间】:2017-11-14 13:55:22
【问题描述】:
    if what == "decode":    
    dog_input = input("What would you like to decode?")
    letternumber = len(dog_input)
    a=(dog_input)
    for i in range(a):    
        if dog_input[1] == "1":
            print("a")
        if dog_input[1] == "2":
            print("b")
        if dog_input[1] == "3":
            print("c")
        if dog_input[1] == "4":
            print("d")
        if dog_input[1] == "5":
            print("e")
        if dog_input[1] == "6":
            print("f")
        if dog_input[1] == "7":
            print("g")
        if dog_input[1] == "8":
            print("h")
        if dog_input[1] == "9":
            print("i")
        if dog_input[2] == "10":
            print("j")
        if dog_input[2] == "11":
            print("k")
        if dog_input[2] == "12":
            print("l")
        if dog_input[2] == "13":
            print("m")
        if dog_input[2] == "14":
            print("n")
        if dog_input[2] == "15":
            print("o")
        if dog_input[2] == "16":
            print("p")
        if dog_input[2] == "17":
            print("q")
        if dog_input[2] == "18":
            print("r")
        if dog_input[2] == "19":
            print("s")
        if dog_input[1] == "20":
            print("t")
        if dog_input[2] == "21":
            print("u")
        if dog_input[2] == "21":
            print("v")
        if dog_input[2] == "22":
            print("w")
        if dog_input[2] == "23":
            print("x")
        if dog_input[2] == "24":
            print("y")
        if dog_input[2] == "25":
            print("z")
        if dog_input[3] == "-64":
            print(" ")

这段代码什么都不做——它会随机打印一个字符三到四次。我希望用户输入数字并取出字母,但它拒绝阅读。我如何让它发挥作用?另外,有没有更快的方法给字母分配数字?

【问题讨论】:

  • 您能否修复缩进并缩短代码以仅显示基本行为?另外,如果您给我们一个使用示例和预期结果,您更有可能获得帮助
  • 您能解释一下您是如何为每个条件句选择索引的吗?为什么if dog_input[2] == "19":使用索引2,而if dog_input[1] == "20":使用索引1
  • 你的序列中的错误是故意的吗?您有两次"21",您正在测试dog_input[1]dog_input[2]dog_input[3],似乎是随机的,并且忽略了for 循环中的i
  • 也许还提供所需输入和输出的示例
  • 另外,索引到您的dog_input 字符串将给出单个字符,因此任何对两个字符字符串(2 位数字)的测试都将失败。

标签: python


【解决方案1】:

问题:

for i in range(a) :

这将逐字母迭代字符串或数组,因此不会检测到两位输入。示例:输入 12 将被视为两个单独的输入 1 和 2。

解决方案:

将输入作为空格分隔的数字,并通过将空格视为分隔符来分隔它。看下面sn-p

dog_input_final = dog_input.split(" ")

以下代码(仅循环部分)也解决了这个问题,它是为字符分配数字的更好方法。

dog_input_final = dog_input.split(" ")
for i in dog_input_final:
    if(i == "-64"):
        print " "
    else :
        print chr(97+int(i))

【讨论】:

    【解决方案2】:

    您可以创建一个字典来存储字母及其数字:

    dictionary = {'a':1,'b':2,'c':3,'d':4,'e':5,'f':6,'g':7,'h':8,'i':9,'j':10,'k':11,'l':12,'m':13,'n':14,'o':15,'p':16,'q':17,'r':18,'s':19,'t':20,'u':21,'v':22,'w':23,'x':24,'y':25,'z':26,' ':-64} 
    

    然后使用 Series 根据您的 case letternumber 中的值获取密钥:

    lookup_list = pd.Series(dictionary)
    print(lookup_list[lookup_list.values == letternumber ].index)
    answer = lookup_list[lookup_list.values == 19].index
    

    【讨论】:

      猜你喜欢
      • 2011-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-12
      • 2017-01-27
      • 1970-01-01
      • 2020-04-09
      相关资源
      最近更新 更多