我认为使用变量将是更有效的方法来解决这个问题。观察:
print(""""
1. strength \n
2 Health\n
3. wisdom\n
4. dexterity
5. remove 1 strength for point
6. remove 1 health for point
7. Remove 1 wisdom for point
8. remove 1 dexterity for point
""")
points=30
strength=0
health=0
wisdom=0
dexterity=0
default=1
while default:
choice=int(input("Select attributes that you want:"))
if choice==1:
strength+=1
points-=1
print("Strength: ", strength, "\nHealth: ", health, "\nWisdom: ", wisdom, "\nDexterity: ")
elif choice==2:
health+=1
points-=1
print("Strength: ", strength, "\nHealth: ", health, "\nWisdom: ", wisdom, "\nDexterity: ", dexterity)
elif choice==3:
wisdom+=1
points-=1
print("Strength: ", strength, "\nHealth: ", health, "\nWisdom: ", wisdom, "\nDexterity: ", dexterity)
elif choice==4:
dexterity+=1
points-=1
print("Strength: ", strength, "\nHealth: ", health, "\nWisdom: ", wisdom, "\nDexterity: ", dexterity)
elif oc==5:
strength-=1
points+=1
print("Strength: ", strength, "\nHealth: ", health, "\nWisdom: ", wisdom, "\nDexterity: ", dexterity)
elif oc==6:
health-=1
points+=1
print("Strength: ", strength, "\nHealth: ", health, "\nWisdom: ", wisdom, "\nDexterity: ", dexterity)
elif oc==7:
wisdowm-=1
points+=1
print("Strength: ", strength, "\nHealth: ", health, "\nWisdom: ", wisdom, "\nDexterity: ", dexterity)
elif oc==8:
dexterity-=1
points+=1
print("Strength: ", strength, "\nHealth: ", health, "\nWisdom: ", wisdom, "\nDexterity: ", dexterity)
当然,你也可以使用字典,但变量的想法更容易,编程是关于效率。
我宁愿第二个,因为那么你可以循环通过 – Doorknob
为了更灵活的方法:属性的集合看起来非常适合类,比字典或一堆变量更适合 –
我还没有得到在这本书中尚未有类,但是我已经看到了它们的一些例子。所以如果我要做一个'def attributes:'类并且在那里分配所有的强度,健康等值。我可以稍后打电话给他们,或者用attributes.strength,attributes.health等来改变他们。 – mccdlibby