基本上我设置了我的程序,以便它接收名称,然后将它们分开,直到它达到1到9之间的数字,所以如果数字是35,它将分裂为5和3,这给出了8.我的问题与我的程序是,如果我要求只输入一个名称,它会给出一个错误,但2和更高的名称有效吗?它也必须工作,如果数字上升到325,因为它需要3 + 2 + 5然后之后它将是10然后再分裂给1 + 0然后是1.最后我也希望能够有一个含义=“”而且从1到9的totalValue会有一个含义,就像这个值意味着你的强者或思想家或好的演讲者或其他什么。主要问题是名称列表是否有一种方法可以绕过num_names中只有一个数字的限制,这样我可以一直到无限数字?我该怎么办? 我相信这段代码可能会有所帮助,但我不确定。 list_names = list(names)
def name_value(name):
return sum((ord(chr) - 65) % 9 + 1 for chr in name)
totalValue = 0
for list_name in names:
totalValue += name_value(list_name)
我不知道在哪里放这个
import sys
print("Please enter each name when asked without any spaces.") #The program will post this
print("Please enter each of your names individually also.") #Program will again post this
names = [] #This is the value of names which will be changed depending on the input
currentnum = 0 #Currentnum value is 0
while True:
try:
num_names = int(input("Enter number of names: "))
break
except ValueError:
print("That's not a number!")
for i in range(num_names):
name = input("Enter name " + str(i) + " :")
name = name.upper()
while True:
if name.isalpha():
break
else:
name = input("Enter name number " + str(i) + " again:")
names.append(name)
num1 = ["A", "J", "S"]
num2 = ["B", "K", "T"]
num3 = ["C", "L", "U"]
num4 = ["D", "M", "V"] #This is a dictionary giving all the upper case letters a value of num1, num2 etc
num5 = ["E", "N", "W"]
num6 = ["F", "O", "X"]
num7 = ["G", "P", "Y"]
num8 = ["H", "Q", "Z"]
num9 = ["I", "R"]
totalValue = 0
list_names = list(names)
first_name = list_names[0]
second_name = list_names[1]
#third_name = list_names[2]
print(first_name)
print(second_name)
#print(third_name)
print(list_names)
for chr in first_name: #if, elif and else code
if (chr in num1):
totalValue += 1
elif (chr in num2):
totalValue += 2
elif (chr in num3):
totalValue += 3
elif (chr in num4):
totalValue += 4 #This gives TotalValue(the number answer) depending on the letter
elif (chr in num5):
totalValue += 5
elif (chr in num6):
totalValue += 6
elif (chr in num7):
totalValue += 7
elif (chr in num8):
totalValue += 8
elif (chr in num9):
totalValue += 9
else:
print("program failure")
for chr in second_name: #if, elif and else code
if (chr in num1):
totalValue += 1
elif (chr in num2):
totalValue += 2
elif (chr in num3):
totalValue += 3
elif (chr in num4):
totalValue += 4 #This gives TotalValue(the number answer) depending on the letter
elif (chr in num5):
totalValue += 5
elif (chr in num6):
totalValue += 6
elif (chr in num7):
totalValue += 7
elif (chr in num8):
totalValue += 8
elif (chr in num9):
totalValue += 9
else:
print("You have entered invalid inputs for names. Program will Now end.")
values = []
totalCount = 0
for x in values: #This will activate or iterate the loop for the value
totalCount = totalCount + x #This will add all the values together and to give a totalcount
while len(str(totalValue)) != 1: #The code will split the 2 digit number until the addition equals to 1 digit
num = 0
for x in str(totalValue): #This is the value x in the string totalcount
num = num + int(x) #This means that the number being output will have the int of the value x
totalValue = str(num) #The totalCount must equal to the string of num
#[int(d) for d in str(totalValue)]
print(totalValue)
答案 0 :(得分:0)
好吧,我想我不是你想要的。 看看:
import sys
def sumString2Digit(the_char):
d = ord(the_char.upper()) - 65 + 1
r = 0
while d:
r, d = r + d % 10, d // 10
if d == 0 and r >= 10 :
d = r
r = 0
return r
#all_names = "John Smith" #Expected output: 44
all_names = raw_input("Enter ALL names separated by spaces: ")
names = all_names.split(' ')
res = []
for name in names:
res.append(sum(map(lambda x: sumString2Digit(x), name)))
print ("the code for {} is {}".format(all_names, sum(res)))
示例:强>
$python <scriptName>
$Enter ALL names separated by spaces: John Smith
$the code for John Smith is 44