2017-03-20 47 views
0

基本的に私のプログラムは名前を取り、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) 
+0

1つの名前は有効なオプションですか? > 1が入力されるまで、例外ハンドラを使用しないでください。 –

+0

ここに貼り付けたコードを修正する必要があります。これは正しくインデントされません。 – Tatsh

+0

あなたのコードでは少なくとも2つの名前が必要です:second_name = list_names [1]、その入力を最初に検証する必要があります。ロジックがデバッグされ、コードが失敗する前に行の値を検査してください。 –

答えて

0

当面の問題以下のあなたのラインで

を、あなたは、少なくとも二つの名前が必要です。

list_names = list(names) 

first_name = list_names[0] 
second_name = list_names[1] 
#third_name = list_names[2] 

second_name割り当てが失敗したことを、何の2番目の名前がない場合。私はあなたのプログラムが何をすべきかを記述していないし、実際の問題点に集中していないので、これ以上は役に立たない。あなたはあなたの全体的なリストを持っているしかし、多くの名前を順番に必要

SOLUTION

。代わりに、このようなものを使用していないハードコードになどFIRST_NAMEsecond_nameをください:

for name in list_names: 
    for chr in name: 
     ... the part you already know how to do ... 

MORE UPGRADES

あなたの値の変換にも一般化する必要が。代わりに、これらの3文字のリストを、値に各文字変換辞書使用:今

score = { 
    'A': 1, 'B': 2, 'C':3, ... 
} 

を、あなたは、単にこの後

totalValue += score[chr] 

で文字値を取得する概念を調べることができますリストの理解の合計メソッドをスコアのリストに使用してください。これらを習得すれば、現在の実装よりもはるかに読みやすいコードの1行で各名前の値を計算できます。

+0

基本的には、今のところ1単語で動作するようにプログラムを作りたいです。だから私はそれがfirst_name second_nameと関係があると信じていますので、できるだけ多くの名前に1から多くの名前を入れる必要があるので、私は何をしますか?しかし、もちろんそれは2番目の名前が必要なので、少なくとも2つの名前を持つように強制するのではなく、少なくとも1つの名前しか持たないようにしてください。 –

+0

私はそれが3で動作しないことを認識しましたそれは2と3で動作しますそれは基本的にどのように私はそれを得るので、必要な単語の量が設定されていません –

+0

基本的に他の答えとthatsどのように私はそれを修正した。 –

0

私はこれまでにこのコードを見てきました。実際に解決しようとしている問題ではなく、実際に解決しようとしている問題を伝えたいと思うかもしれません。

あなたがして、あなたの長いif/else秒のほとんどを置き換えることができます。

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.") 

コンピュータがあなたのために数学をやってみましょう:

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) 

は基本的に同じです。

+0

TemporalWolf私はこれをして固定しましたが、今度は再び働かずに0の値を与えました。私はシステムが3桁の数字、例えば353になり、分割できなかったと思います。 –

+0

@StackOverflowUserあなたの改訂コードであなたの質問を更新してください。 – TemporalWolf

+0

私はしばらくの間働いて突然ブームを起こし、値が0になり始めました –

関連する問題