Python 如何将一个数字一分为二而不是除以
基本上,我的程序是这样设置的,它接受名字,然后把它们分开,直到它变成一个介于1和9之间的数字,所以如果这个数字是35,它会分成5和3加起来,得到8。我的程序的问题是,如果我要求只输入一个名称,它会给出一个错误,但2个以上的名称有效吗?我该怎么办Python 如何将一个数字一分为二而不是除以,python,python-3.x,Python,Python 3.x,基本上,我的程序是这样设置的,它接受名字,然后把它们分开,直到它变成一个介于1和9之间的数字,所以如果这个数字是35,它会分成5和3加起来,得到8。我的程序的问题是,如果我要求只输入一个名称,它会给出一个错误,但2个以上的名称有效吗?我该怎么办 import sys print("Please enter each name when asked without any spaces.") #The program will post this print("Plea
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"]
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)
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)
直接问题 在下面的行中,您至少需要两个名称:
list_names = list(names)
first_name = list_names[0]
second_name = list_names[1]
#third_name = list_names[2]
如果没有第二个名称,则分配将失败。我无能为力,因为你没有描述你的程序应该做什么,也没有关注实际的问题点
解决方案
你需要循环浏览你在整个列表中有多少名字。不要硬编码第一个\u名称,第二个\u名称等。相反,请使用以下内容:
for name in list_names:
for chr in name:
... the part you already know how to do ...
更多升级
你的价值翻译也需要推广。使用字典将每个字母翻译成一个值,而不是这三个字母列表:
score = {
'A': 1, 'B': 2, 'C':3, ...
}
现在,您只需使用
totalValue += score[chr]
在此之后,查找列表理解的概念,并对分数列表使用求和方法。如果您了解了这些,就可以在一行代码中计算每个名称的值,比您当前的实现可读性强得多。我以前看过这段代码。。。你可能想知道的是你到底想解决什么问题,而不是你认为什么能让它起作用 您可以将大部分长
if/else
s替换为:
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)
基本相当于:
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"]
list_names = list(names)
first_name = list_names[0]
second_name = list_names[1]
#third_name = list_names[2]
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.")
让计算机为您计算。一个名字是有效的选项吗?如果输入大于1,则不使用异常处理程序。您应该在此处修复粘贴的代码。它的缩进不正确。您的代码显然需要至少两个名称:second_name=list_name[1],因此您需要首先验证该输入。请尝试在代码失败之前调试逻辑并检查行中的值。因此,基本上我希望程序能够与1个字一起工作,因为目前它不能。所以我相信这与第一个名字和第二个名字有关,所以我该怎么做,因为我需要程序尽可能多地接收1个名字。但是当然它需要第二个名字,所以我不强迫它至少有两个名字,并且至少有一个名字,我还意识到它不适用于3,它只适用于2,至于第三个名字,它是散列出来的,所以基本上我如何得到它,所以没有固定数量的单词需要基本上仰望另一个我做了这件事,它修好了,但现在它不再工作了,给了我0的值,我想我破坏了这个系统,因为它变成了三位数,比如353,但它不能split@StackOverflowUser用你修改过的代码更新你的问题。好吧,我会让它运行一段时间,然后突然我启动了正在获取值0