プログラムはあなたにフルネームを尋ねてそれを数字に変換することを想定しています、そしてそれはそれが1桁の数字を得るまでリストに数字を追加し続けます。製品を完成させて問題なく動作しましたが、3桁以上を追加できません。助けてください。リンクのどこに助けが必要か
name1 = [] #its a list called name
name1 = str(raw_input("Enter your first name and second name ")) #this code asks the user and saves the name as a list
lettervalue =[]
index=0
#
for index in range (len(name1)):
if name1 [index] == "a" or name1 [index] == "j" or name1 [index] == "s":
lettervalue.append(1)
elif name1 [index] == "b" or name1 [index] == "k" or name1 [index] == "t":
lettervalue.append(2)
elif name1 [index] == "c" or name1 [index] == "l" or name1 [index] == "u":
lettervalue.append(3)
elif name1 [index] == "d" or name1 [index] == "m" or name1 [index] == "v":
lettervalue.append(4)
elif name1 [index] == "e" or name1 [index] == "n" or name1 [index] == "w":
lettervalue.append(5)
elif name1 [index] == "f" or name1 [index] == "o" or name1 [index] == "x":
lettervalue.append(6)
elif name1 [index] == "g" or name1 [index] == "p" or name1 [index] == "y":
lettervalue.append(7)
elif name1 [index] == "h" or name1 [index] == "q" or name1 [index] == "z":
lettervalue.append(8)
elif name1 [index] == "i" or name1 [index] == "r":
lettervalue.append(9)
elif name1 [index] == " ":
lettervalue.append(0)
index = index + 1
#
print lettervalue #prints the list to reduce confusion
total1 = sum(lettervalue) #sums up the numbers in the list and turns it into a variable
print total1 # also prints the total to reduce confusion
#
while total1 > 9:
split1 = 0
split2 = 0
total1 = str (total1)
split1 = total1[0]
split2 = total1[1]
total1 = int (split1) + int(split2)
print "your lucky number is " + str(total1)
#
if total1 == 1:
print "WOW you are a Natural leader!"
if total1 == 2:
print "WOW you are a Natural peacemaker!"
if total1 == 3:
print "WOW you are Creative and optimistic !"
if total1 == 4:
print "WOW you are a Hard worker!"
if total1 == 5:
print "WOW you Value freedom!"
if total1 == 6:
print "WOW you one of them who cares and provides!"
if total1 == 7:
print "WOW you are a Think!"
if total1 == 8:
print "WOW you have diplomatic skills!"
if total1 == 9:
print "WOW you are selfless and generous!"
次のコードは、whileループの問題に対処します。
totals = []
total = sum(lettervalue)
while total > 9:
breakdown = list(str(total))
for i in breakdown:
i = int(i)
totals.append(i)
total1 = sum(totals)
これは、プログラムの残りの部分で機能します。さらにエラーが発生した場合は、遠慮なくコメントでお知らせください。
編集:for i in breakdown
コードの一部は、内訳の反復を意味します。
Breakdown
は配列であるため、値が格納されています。forループは、これらすべての値を繰り返し処理し、それぞれに対して同じプロセスを実行します。したがってbreakdown
、値「1、2、3、4」が含まれている場合、コードはその数値を整数に変換し、それを合計配列に追加します。
注:コードのこの部分は、sum
関数が正しく機能するために必要です。リストは整数ではなく文字列を返すため、すべての値を整数に変更する必要があります
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加