社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

在Python中将多个输入项打印为一个显示

Devin Cassidy • 3 年前 • 1206 次点击  

我在Visual Studio代码中编写了这段代码,并在终端中运行了这段代码,我可以为列表输入一个条目,在输入第一个条目后,它会显示数据,但它不允许我一次输入多个条目,而无需再次在终端中运行文件来输入另一个条目。 在我输入这三个条目之后,我试图将这三个条目打印一次。有什么建议/技巧/窍门吗?

# This imports the sys module.
import sys

# This Retail Item class holds data about products.
class RetailItem:

    # __init__ method initializes the attributes.
    def __init__(self, description, units, price):
        self.__item_description = description
        self.__units_in_inventory = units
        self.__price = price

    # The set_item_description method gets the item type.
    def set_item_description(self, description):
        self.__item_description = description

    # The set_units_in_inventory method gets number of items available.
    def set_units_in_inventory(self, units):
        self.__units_in_inventory = units

    # The set_price method gets the cost of item.
    def set_price(self, price):
        self.__price = price

    # The get_item_description method returns the item type.
    def get_item_description(self):
        return self.__item_description

    # The get_units_in_inventory returns the number of items available.
    def get_units_in_inventory(self):
        return self.__units_in_inventory

    # The get_price method returns the cost of item.
    def get_price(self):
        return self.__price


# This is the main function, it gets a list of Retail Items, and then displays them. 

def main():
    
    inventory = make_list()

    print('Here is the Retail Item list you entered: ')
    display_list(inventory)

    # The make_list will get data for three items. It will
    #return a list of available items.

def make_list():
    # Create an empty list.
    item_list = []

    # Add three item to the list.
    print('Enter data for three items: ')
    for count in range(1, 4):
        # Get item data.
        print('Item number ' + str(count) + ':')
        item = input('Enter description of item: ')
        units = float(input('Enter number of units in inventory: '))
        price = float(input('Enter price per item: '))
        print()

        # Creat new RetailItem and assign items variable.
        items = RetailItem(item, units, price)
        # Add items to list.
        item_list.append(items)

        return item_list

# This display_list function displays the items.

def display_list(item_list):
    for item in item_list:
        print(item.get_item_description())
        print(item.get_units_in_inventory())
        print(item.get_price())
        print()

# This calls the main function.
main()
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/131152
 
1206 次点击  
文章 [ 1 ]  |  最新文章 3 年前