【发布时间】: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