我有一个公式,试图把它放到python中,这就是我到目前为止,它的问题是什么?

时间:2014-04-21 14:52:19

标签: python

首先,我想为这个公式制作一个程序:     “所有节点的容量X 4 X node_type X node_feature X 1.aspect_count

这是我到目前为止所做的:

    import math
node_type = 1
node_feature = 1
aspect_count = 0
capacity = 0
def capacity():
    capcaity = int(input("input the capacity of your node: "))

def aspect_count():
    aspect_count = int(input("input the number of aspects that their are: ")) / 10 + 1

def node_type():
    node_type = raw_input("input the type of node you have e.g. pale/bright/normal: ")

def node_feature():
    node_feature = raw_input("input the feature of your node e.g. hungry/pure/normal:")


def which_node_type():
    if node_type == pale:
        node_type = 1.1
    elif node_type == bright:
        node_type = 1.5
    else:
        node_type = 1.3

def which_node_feature():
    if node_feature == hungry:
        node_feature = 5
    elif node_feature == sinister:
        node_feature = 3
    elif node_feature == pure:
        node_feature = 4
    else:
        node_feature = 2.5

def price():
    price = capacity * 4 * which_node_type * which_node_feature * aspect_count

嗯,这就是我到目前为止所得到的但是它引发了elif语法错误的问题,我只是想知道是否有人可以帮我解决这个问题?提前谢谢!

编辑:现在我已经更改了elif行,我收到了这个错误:

追踪(最近一次通话):   文件“”,第40行,in   文件“”,第38行,价格 TypeError:*:'function'和'int'

的不支持的操作数类型

上述任何帮助?

2 个答案:

答案 0 :(得分:2)

您正在错误地编写elif语句。一切都是这样的:

elif:
    node_feature == sinister:

需要像这样写:

elif node_feature == sinister:

换句话说,要评估的条件就在elif关键字之后。

答案 1 :(得分:0)

另请注意,您实际上并未调用price()中的函数 - 应该是

def price():
    price = capacity() * 4 * which_node_type() * which_node_feature() * aspect_count()

并且您的函数没有返回值;你需要return这样:

def capacity():
    return int(input("input the capacity of your node: "))

最后,您的代码有点非结构化;我会把它重写为一个类,如下:

# assumes Python 2.x
def get_int(prompt):
    while True:
        try:
            return int(raw_input(prompt))
        except ValueError:
            pass

def get_one_of(prompt, values):
    while True:
        result = raw_input(prompt).strip()
        if result in values:
            return result
        else:
            print("  (must enter one of [{}])".format(", ".join(values)))

class Node(object):
    node_type_values = {"pale": 1.1, "normal": 1.3, "bright": 1.5}
    node_types = sorted(node_type_values.keys())

    node_feature_values = {"normal": 2.5, "sinister": 3, "pure": 4, "hungry": 5}
    node_features = sorted(node_feature_values.keys())

    @classmethod
    def from_prompt(cls):
        capacity     = get_int("Enter the node's capacity: ")
        node_type    = get_one_of("Enter the node's type: ", cls.node_types)
        node_feature = get_one_of("Enter the node's feature: ", cls.node_features)
        num_aspects  = get_int("Enter the node's aspect count: ")
        return cls(capacity, node_type, node_feature, num_aspects)

    def __init__(self, capacity, node_type, node_feature, num_aspects):
        self.capacity = capacity
        self.node_type = node_type
        self.node_feature = node_feature
        self.num_aspects = num_aspects

    def price(self):
        return 4 * self.capacity * Node.node_type_values[self.node_type] * Node.node_feature_values[self.node_feature] * (1. + 0.1 * self.num_aspects)

def main():
    n = Node.from_prompt()
    print("Price is ${:0.2f}.".format(n.price()))

if __name__=="__main__":
    main()