基本的に私のプログラムは名前を取り、1から9までの数になるまで分割するように設定されているので、35の場合分割されます私のプログラムで私の問題は、私は1つだけの名前を入力するように頼むと、それはエラーを与えるが、2つ以上の名前が機能するということです?私は何をしますか?半分の数値を分割して分割する方法
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)
1つの名前は有効なオプションですか? > 1が入力されるまで、例外ハンドラを使用しないでください。 –
ここに貼り付けたコードを修正する必要があります。これは正しくインデントされません。 – Tatsh
あなたのコードでは少なくとも2つの名前が必要です:second_name = list_names [1]、その入力を最初に検証する必要があります。ロジックがデバッグされ、コードが失敗する前に行の値を検査してください。 –