name = input("Please enter your name: ")
name = name.lower()
luckynumb = 0
firstnamenumb = 0
surnamenumb = 0
number = [1, 2, 3, 4, 5, 6, 7, 8, 9]
row1 = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
row2 = ["j", "k", "l", "m", "n", "o", "p", "q", "r"]
row3 = ["s", "t", "u", "v", "w", "x", "y", "z"]
for letter in name:
if letter == "a" or letter == "j" or letter == "s=":
luckynumb += 1
if letter == "b" or letter == "k"or letter == "t":
luckynumb += 2
if letter == "c" or letter == "l"or letter == "u":
luckynumb += 3
if letter == "d" or letter == "m"or letter == "v":
luckynumb += 4
if letter == "e" or letter == "n"or letter == "w":
luckynumb += 5
if letter == "f" or letter == "o"or letter == "x":
luckynumb += 6
if letter == "g" or letter == "p"or letter == "y":
luckynumb += 7
if letter == "h" or letter == "q"or letter == "z":
luckynumb += 8
if letter == "i" or letter == "r":
luckynumb += 9
surnamenumb = - luckynumb
while int(firstnamenumb) > 9:
split = list(str(firstnamenumb)) # it will
firstnamenumb = int(split[0]) + int(split[1])
print(firstnamenumb)
while int(surnamenumb) > 9:
split = list(str(surnamenumb)) # it will
surnamenumb = int(split[0]) + int(split[1])
print(surnamenumb)
luckynumb = firstnamenumb + surnamenumb
for luckynumb in range(1, 5, 9): #
print(luckynumb)
if int(luckynumb) == 1:
print("Natural Leaders")
elif int(luckynumb) == 2:
print("Natural Peacemakers")
elif int(luckynumb) == 3:
print("Creative and Optimistic")
elif int(luckynumb) == 4:
print("Hard Workers")
elif int(luckynumb) == 5:
print("Value Freedom")
elif int(luckynumb) == 6:
print("Carers and Providers")
elif int(luckynumb) == 7:
print("Thinkers")
elif int(luckynumb) == 8:
print("Have Diplomatic Skills")
elif int(luckynumb) == 9:
print("Selfless and Generous")
else:
print("Error. Please try again")
此代码有什么问题。无论你输入什么内容,只能给出相同的答案。
答案 0 :(得分:2)
这应该适用于您的目的:
name = input("Please enter your name: ")
name = name.lower()
luckynumb = 0
firstnamenumb = 0
surnamenumb = 0
number = [1, 2, 3, 4, 5, 6, 7, 8, 9]
row1 = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
row2 = ["j", "k", "l", "m", "n", "o", "p", "q", "r"]
row3 = ["s", "t", "u", "v", "w", "x", "y", "z"]
for letter in name:
if letter == "a" or letter == "j" or letter == "s": luckynumb += 1
elif letter == "b" or letter == "k" or letter == "t": luckynumb += 2
elif letter == "c" or letter == "l" or letter == "u": luckynumb += 3
elif letter == "d" or letter == "m" or letter == "v": luckynumb += 4
elif letter == "e" or letter == "n" or letter == "w": luckynumb += 5
elif letter == "f" or letter == "o" or letter == "x": luckynumb += 6
elif letter == "g" or letter == "p" or letter == "y": luckynumb += 7
elif letter == "h" or letter == "q" or letter == "z": luckynumb += 8
elif letter == "i" or letter == "r": luckynumb += 9
surnamenumb = - luckynumb
while int(firstnamenumb) > 9:
split = list(str(firstnamenumb)) # it will
firstnamenumb = int(split[0]) + int(split[1])
print(firstnamenumb)
while int(surnamenumb) > 9:
split = list(str(surnamenumb)) # it will
surnamenumb = int(split[0]) + int(split[1])
print(surnamenumb)
luckynumb = firstnamenumb + surnamenumb
luckynumb = abs(luckynumb) % 9
print(luckynumb)
if int(luckynumb) == 0: print("Natural Leaders")
elif int(luckynumb) == 1: print("Natural Peacemakers")
elif int(luckynumb) == 2: print("Creative and Optimistic")
elif int(luckynumb) == 3: print("Hard Workers")
elif int(luckynumb) == 4: print("Values Freedom")
elif int(luckynumb) == 5: print("Careers and Providers")
elif int(luckynumb) == 6: print("Thinkers")
elif int(luckynumb) == 7: print("Has Diplomatic Skills")
elif int(luckynumb) == 8: print("Selfless and Generous")
else: print("Error! Please try again")
另外,查看PEP-8样式指南for python。这将有助于我保证: https://www.python.org/dev/peps/pep-0008/
答案 1 :(得分:1)
name = input("Please enter your name: ").lower().split()
firstname, *middle, lastname = name
first_num = sum(((ord(letter) - 97) % 9) + 1 for letter in firstname)
sur_num = sum(((ord(letter) - 97) % 9) + 1 for letter in lastname)
while first_num > 9:
first_num = sum(map(int, str(first_num[:2])))
while sur_num > 9:
sur_num = sum(map(int, str(sur_num[:2])))
lucky = first_num + sur_num
print("Your lucky number is {}".format(lucky))
这是我尝试整理你的代码。我简化了每个字母值的计算。 ord
获取每个数字的序数值,然后我们将除数的余数除以9,然后加1,这样我们的结果就不会从0开始。
然后我们将数字的前两位数字加在一起,直到它小于9.如果您想要对所有数字求和,只需删除[:2]
。
作为附录,你确定在将它们减少到10以下之后,你应该将姓氏和名字的总和加起来吗?我认为首先这样做会更有意义,然后减少总和。