如何创建用户定义的变量数

时间:2015-11-21 20:37:22

标签: python loops variables

我试图编写一个程序,允许用户输入给定数量的计算机的规格。然后,我将使用公式计算总体值'那些使用指定规格的计算机。

然而,我的问题是我不知道如何创建那么多变量。

到目前为止,这是我的代码:

print("Best Computer Program")
int1=input("Enter an integer between 1 and 10000\n")

for x in range(int1)

    name=input("Enter your computer's name:\n")
    R=input("Enter the amount of RAM you want in GB: (1-128)\n")
    S=input("Enter the CPU speed of your computer in Hz: (1-4000)\n")
    D=input("Enter the disk drive space: (1-3000)\n")
    print()

    x 

所以这是计算值的公式:

2 * R + 3 * S + D

但问题是,我必须为用户想要放入的许多计算机做这件事。

感谢您的帮助。

4 个答案:

答案 0 :(得分:1)

这是一个更结构化的版本:

def get_int(prompt, lo=None, hi=None):
    while True:
        try:
            val = int(input(prompt))
            if (lo is None or lo <= val) and (hi is None or val < hi):
                return val
        except ValueError:
            # not an integer; try again
            pass

class Computer:
    __slots__ = ["name", "ram", "speed", "disk"]

    @classmethod
    def from_prompt(cls):
        name  =   input("Enter your computer's name: ")
        ram   = get_int("Enter the amount of RAM you want in GB (1-128): ",      1,  129)
        speed = get_int("Enter the CPU speed of your computer in Hz (1-4000): ", 1, 4001)
        disk  = get_int("Enter the disk drive space in GB (1-3000): ",           1, 3001)
        return cls(name, ram, speed, disk)

    def __init__(self, name, ram, speed, disk):
        self.name  = name
        self.ram   = ram
        self.speed = speed
        self.disk  = disk

    def value(self):
        return 2 * self.ram + 3 * self.speed + self.disk

def main():
    num_machines = get_int("How many computers do you have? ", 0, 10000)
    machines = [Computer.from_prompt() for _ in range(num_machines)]
    machines.sort(key = lambda m: m.value(), reverse = True)
    # machines is now sorted from most- to least-valuable

if __name__=="__main__":
    main()

答案 1 :(得分:0)

创建元组列表,其中第一项是排名。对列表进行排序并从顶部获取。顺便说一句,input给出了需要转换为整数的字符串。

int1=int(input("Enter an integer between 1 and 10000\n"))

machines = []

for x in range(int1)

    name=input("Enter your computer's name:\n")
    R=int(input("Enter the amount of RAM you want in GB: (1-128)\n"))
    S=int(input("Enter the CPU speed of your computer in Hz: (1-4000)\n"))
    D=int(input("Enter the disk drive space: (1-3000)\n"))
    machines.append((2 * R + 3 * S + D, name, R, S D))

machines.sort(reverse=True)

答案 2 :(得分:0)

虽然可以通过简单的方式解决这个问题,但我们也可以使用object-oriented programming来解决这个问题。我们将创建一个Computer类,它保存有关给定计算机的数据(类的“实例”),将所有计算机放入列表中,然后对列表进行排序。

class Computer():
    # Classes are made up of attributes and functions.
    def __init__(self):
        # We initialize the instance by getting all of the information,
        # and store it as class attributes.
        self.name=input("Enter your computer's name:\n")
        self.ram=int(input("Enter the amount of RAM you want in GB: (1-128)\n"))
        self.cpu=int(input("Enter the CPU speed of your computer in Hz: (1-         4000)\n"))
        self.drive=int(input("Enter the disk drive space: (1-3000)\n"))
        # Now, we get the value (notice that this is its own function)
        self.get_value()

    def get_value(self):
        self.value = 2 * self.ram + 3 * self.cpu + self.drive

print("Best Computer Program\n")
num_computers = int(input("Enter an integer between 1 and 10000\n"))
computers_list = []
for _ in range(num_computers):
    # This is the point at which we create a new instance of the Computer
    # class.
    computers_list.append(Computer())

# Once we have all of the computers, we sort them.

# The 'lambda x: x.value' sorts the list by x.value
computers_list.sort(key=lambda x: x.value, reverse=True)

# Print the name and value of the first 2 computers.
print([(x.name, x.value) for x in computers_list[0:2]])

答案 3 :(得分:-1)

尝试

导入此

它应该有助于确定最适合您需求的解决方案:)