[英]Assigning numbers to letters in Python
This is aa plan of what i want to do with the code.这是我想用代码做什么的计划。
This is all I have to offer, I'm not sure if it's correct.这就是我要提供的全部内容,我不确定它是否正确。
input ("What's your name?")
[A,J,S]=1
[B,K,T]=2
[C,L,U]=3
[D,M,V]=4
[E,N,W]=5
[F,O,X]=6
[G,P,Y]=7
[H,Q,Z]=8
[I,R]=9
Input a person's name输入一个人的名字
Calculate their lucky name number using the grid in Figure 1 above使用上面图 1 中的网格计算他们的幸运姓名编号
Display the names显示名称
Display the lucky name number显示幸运姓名号码
Firstly, the input value needs to be assigned to a variable.首先,需要将输入值分配给一个变量。 So instead of doing所以而不是做
input ("What's your name?")
You should write你应该写
input_text = input("What's your name?")
This will store the user's input to the string variable input_text.这会将用户的输入存储到字符串变量 input_text 中。
Next, we will need to declare a dictionary to map each letter to its number.接下来,我们需要声明一个字典来将每个字母映射到它的数字。
alpha_map = {'A': 1, 'B': 2, 'C': 3} # And so on until 'Z'
After we have done this, we can iterate through each character of input_text and grab its value from the dictionary.完成此操作后,我们可以遍历 input_text 的每个字符并从字典中获取其值。
alpha_values = [] # An empty list
for character in input_text:
alpha_values.append(alpha_map[character]) # Adds each character's value to alpha_values.
You can use the sum() function to sum up the list of values.您可以使用 sum() 函数对值列表求和。
As for adding up the digits, you could convert the resulting number into a string and convert each character to an int before summing up.至于数字相加,您可以将结果数字转换为字符串,然后在求和之前将每个字符转换为 int。
total = 0
for character in str(number):
total += int(character)
Note that you will have to strip the input text of any non-alphabetical characters, and capitalize all letters for this program to run - there are already plenty of online documentations explaining how to do this.请注意,您必须去除任何非字母字符的输入文本,并将所有字母大写才能运行该程序 - 已经有大量在线文档解释了如何执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.