我正在嘗試做第5章絕對初學者的Python編程挑戰,並且似乎遇到了一些問題。我正在研究的程序是用字典制作一個角色屬性技能點分佈程序。目標是製作一項有4項技能的項目,您可以分配30個點。您可以添加它們,刪除它們或全部查看它們。當我啓動我的計劃時,它將分數分配給所有技能,而不僅僅是一個,這不是我想要的。此外,它每次都有我的「其他」選項的消息,我也不想。對不起,發佈整個事情,我只是新的這一點,我不確定在我弄亂的代碼中的哪裏。謝謝!Python字典追加問題
# Hero Attribute Assigner
# My attempt
name = ""
attr = {"STRENGTH":0, "DEXTERITY":0, "WISDOM":0, "HEALTH":0}
totalPoints = 30
for attrName in attr:
attrPoints = attr[attrName]
userInput = None
while userInput != "5":
userInput = input \
("""
Character Creator
1 - Name Character
2 - Assign Attribute Points
3 - Remove Attribute Points
4 - Exit
""")
# Name Character
if userInput == "1":
name = str(input("\nCharacter Name: "))
print("\nYour character's name is now: " + name)
input("\nPress 'Enter' to return to menu.")
# Assign Points
if userInput == "2":
# What attribute?
print("\n" + name + "'s Attribute Stats are:\n")
for attrName in attr:
print(attrName, ":\t", str(attrPoints))
print("UNUSED POINTS:" + "\t" + str(totalPoints))
changeAttr = input("\nWhat attribute would you like to add points to? ").upper()
# How many points to add?
if changeAttr in attr:
changePoints = int(input("How many points would you like to add? "))
attr[changeAttr] += changePoints
totalPoints -= changePoints
print("\n" + name + "'s Attribute Stats are now:\n")
for attrName in attr:
print(attrName, ":\t", str(attrPoints))
print("UNUSED POINTS:" + "\t " + str(totalPoints))
else:
print("\nThat is not a valid choice.")
# Remove Points
if userInput == "3":
# What attribute?
print("\n" + name + "'s Attribute Stats are:\n")
for attrName in attr:
print(attrName, ":\t", str(attrPoints))
print("UNUSED POINTS:" + "\t" + str(totalPoints))
changeAttr = input("\nWhat attribute would you like to remove points from? ").upper()
# How many points to remove?
if changeAttr in attr:
changePoints = int(input("How many points would you like to remove? "))
attr[changeAttr] -= changePoints
totalPoints += changePoints
print("\n" + name + "'s Attribute Stats are now:\n")
for attrName in attr:
print(attrName, ":\t", str(attrPoints))
print("UNUSED POINTS:" + "\t " + str(totalPoints))
else:
print("\nThat is not a valid amount.")
# Exit
elif userInput == "4":
break
# Invalid Choice in Menu
else:
print("\nInvalid choice...")
input("\nPress 'Enter' to exit.")
你需要'if/elif/elif/elif/else',但你有'if。如果。如果/ elif/else'。這將解決'無效選擇'的信息。 – TessellatingHeckler