简体   繁体   English

如何将短语打印为随机生成的数字?

[英]How can I print the phrase as the randomly generated numbers?

Code:代码:

import string, random
import pandas as pd
#User Input
title = input("Please Enter A Title For This Puzzle: ")
if len(title) == 0:
    print("String is empty")
    quit()

phrase = input("Please Enter A Phrase To Be Encoded: ")
if len(phrase) == 0:
    print("String is empty")
    quit()


#Numbers get assigned to the letters
nums = random.sample(range(1, 27), 26)
code = dict(zip(nums, string.ascii_lowercase))

#'Start' of Puzzle for the user  
print ("The Title Of This Puzzle Is", title)


#Code for Grid



code2 = {'Number': [[nums[0]],[nums[1]],[nums[2]],[nums[3]],[nums[4]],[nums[5]],[nums[6]],[nums[7]],[nums[8]],[nums[9]],[nums[10]],[nums[11]],[nums[12]],[nums[13]],[nums[14]],[nums[15]],[nums[16]],[nums[17]],[nums[18]],[nums[19]],[nums[20]],[nums[21]],[nums[22]],[nums[23]],[nums[24]],[nums[25]]],
        'Letter': list(string.ascii_lowercase),
        }




df = pd.DataFrame(code2, columns = ['Number', 'Letter'])

print (df)

What I am currently trying to do is take the phrase that has been entered and spit out the randomly generated numbers, under the #Numbers Get Assigned To The Letter comment, instead.我目前正在尝试做的是在#Numbers Get Assigned To The Letter 评论下,取出已输入的短语并吐出随机生成的数字。
Eg: Hello = 20-1-16-16-19例如:你好 = 20-1-16-16-19

I have already tried but the code im writing is nonsense and would like some outside help我已经试过了,但我写的代码是无稽之谈,需要一些外部帮助

Thank you谢谢

If I understand you correctly, is it something like this you were looking for?如果我理解正确,您要寻找的是这样的东西吗?

flip the nums position翻转数字位置

code = dict(zip(string.ascii_lowercase, nums))
code.update({" ":0})
HELLO = [code[item] for item in "hello"]

This may not be the optimal solution but it works.这可能不是最佳解决方案,但它有效。 Hope it helps.希望能帮助到你。

def encode(phrase):     #Takes a phrase and returns the encoded result
    result = []
    for char in phrase:
        result.append(str(code_dict[char.lower()]))
    return '-'.join(result)

nums = random.sample(range(1, 27), 26)  #Creating lists of the random number and letters
letters = list(string.ascii_lowercase)
code_dict = {}

for i in range(len(letters)):    #Creating a dict relating the letters to a number
    key = letters[i]
    value = nums[i]
    code_dict[key] = value

encode(phrase)    #Encoding the phrase

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

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