Py学习  »  Python

在python中为对象中的多个变量赋值

Jakob • 5 年前 • 1507 次点击  

在分配点时有没有一种方法,代码不会重复太多? 另外,代码还没有完成,还需要进行更多的检查。当您第一次分配健康点时,您可以分配超过指定的,然后它要求您分配点,即使没有剩余要分配的点,并且在while循环检查之前,“剩余点”将变为负数,但这是我将尝试自己解决的问题。

class Player:
     def __init__(self, name, health, strength, defence, luck):
         self.name = name
         self.health = health
         self.strength = strength
         self.defence = defence
         self.luck = luck

def playerSetup():
     optio = Player

     name = input(colored("\nWhat is your name soldier? ", "yellow"))
     optio.name = name  


while points > 0:   

    health = int(input("\nType in your Health: "))
    if points > 0:
        optio.health =  health + optio.health
        points = points - optio.health
        print("Points remaining: ", points)

    strength = int(input("\nType in your Strength: "))
    if points > 0:
        optio.strength = optio.strength + strength
        points = points - optio.strength
        print("Points remaining: ", points)

    defence = int(input("\nType in your Defence: "))
    if points > 0:
        optio.defence = optio.defence + defence
        points = points - optio.defence
        print("Points remaining: ", points)

    luck = int(input("\nType in your Luck: "))
    if points > 0:
        optio.luck = optio.luck + luck
        points = points - optio.luck
        print("Points remaining: ", points)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38566
 
1507 次点击  
文章 [ 2 ]  |  最新文章 5 年前