【问题标题】:multiple input numbers for chr()chr() 的多个输入数字
【发布时间】:2020-07-24 04:18:35
【问题描述】:

我正在尝试输入多个数字,例如

[106, 103, 110, 110, 113, 32, 121, 113, 116, 110, 102] 

使用ord()进入解密,然后通过凯撒密码输入。凯撒密码有效,但我不知道如何使用chr(),但它表明当我输入数字时这是一个错误。 谢谢!

    print("Decryption")
    text = int(input("Enter encrypted numbers: \n"))
    encrypt = chr(text)
    decrp_key = int(input("Enter key:\n"))
    decrypted_text = ""
    for i in range(len(encrypt)):
        if ord(encrypt[i]) == 32:
            decrypted_text += chr(ord(encrypt[i]))
        elif ((ord(encrypt[i]) - decrp_key) < 97) and ((ord(encrypt[i]) - decrp_key) > 90):
            temp = (ord(encrypt[i]) - decrp_key) + 26
            decrypted_text += chr(temp)
        elif (ord(encrypt[i]) - decrp_key) < 65:
            temp = (ord(encrypt[i]) - decrp_key) + 26
            decrypted_text += chr(temp)
        else:
            decrypted_text += chr(ord(encrypt[i]) - decrp_key)
    print("Decrypted Text: " + decrypted_text)```

【问题讨论】:

  • 您能否指定您想要的输入/输出。您正在阅读单个 int,但您在描述中指的是整数列表。
  • 我尝试将数字一起输入,然后使用 chr 将其转换为单词以便翻译为凯撒密码。这样我就可以将输出读取为单词。
  • 提示:如果您需要数字列表,则将用户输入,但 append 放入列表中。
  • ValueError: invalid literal for int() with base 10: '106, 103, 110, 110, 113, 32, 121, 113, 116, 110, 102' 这是我得到的错误
  • 你不能在一次输入所有的数字,你必须append他们到一个列表,storeNum= []然后追加到那个列表。

标签: python chr


【解决方案1】:

我认为您想获取一组输入数字并遍历它们。如果您是 python 的新手,您可能需要学习一些命令,但这里有一些您可以使用的东西。

decrp_key = input("Enter key:\n")
decrypted_text = list(map(int,decrp_key.split(',')))
print (decrypted_text)

输出:

>>> Enter key:
>>> 1, 105, 201, 103, 205
>>> [1, 105, 201, 103, 205]

要访问列表,您可以使用 for 循环。

for i in decrypted_text:
    print(i)

输出:

1
105
201
103
205

【讨论】:

    【解决方案2】:

    如果你想输入一个数字列表,那么你需要这样做

    print('Decryption')
    text = ""
    while True:
        temp = input('Enter encrypted number: ')
        if not temp: #break on empty input
            break
        text = text + chr(int(temp))
    decrp_key = int(input("Enter key:\n"))
    #rest of your code goes here...
    

    您还应该使用 try except 块包装您的输入,以确保您的输入是有效的 int!

    【讨论】:

    • 嗨,我遇到了一个值错误,我对 python 很陌生。我不太明白?
    • 你必须一个一个地添加数字。空行停止。如果它有效,请接受这个作为答案
    • 所以不能一次添加多个数字?
    • 如果您是初学者,请先学会使用上述输入功能。之后就可以学习文本操作了。
    • 不,他想为输入提供一个完整的整数列表,他还无法理解如何处理,这不等于在他的代码中附加一个列表。他想将那些 ASCII (int) 值转换为字符,因此不需要任何列表,可以通过循环遍历他读取的字符串来查看。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 2019-10-18
    • 1970-01-01
    相关资源
    最近更新 更多