【发布时间】:2018-09-24 23:38:52
【问题描述】:
我一直在尝试在这个问题上找到自己的方式,但到目前为止还没有找到任何答案。当用户被要求input 一个字符串时,我正在尝试创建一个新的list。我真的可以在这里应用list 命令还是有更好的方法?只是 Python 的初学者。
我的程序会询问有多少库存(例如水果、蔬菜、饮料)。 然后它会询问库存清单的名称。 对于库存的每个名称,它们将包含每个项目的项目,并要求用户输入该特定库存中有多少项目。
这是我的代码:
# MODULES
import os
# FUNCTIONS
def clear():
os.system('cls')
def skip():
input("<Press Enter To Continue>")
def createINV():
clear()
invlist = []
countinv = int(input("Enter the number of Inventories: "))
for x in range(countinv):
# ADD LISTS HERE
# print(("Enter Inventory #%d" % (x+1)), ":")
#typeinv = input(" :")
addinv = input("Enter Inventory #%d: " % (x+1))
invlist.append(addinv)
# invlist.index(x) = [] < ------ function call so i can't assign here
for y in range(countinv):
countitems = int(input("\nHow many items in %s: " % invlist[y]))
for z in range(countitems):
additem = (input("Enter item#%d for %s Inventory: " % ((z+1), invlist[y])))
invlist.index(x).append(additem) # <--- this is where my program stops
#print(invlist[x])
#for x in range(countinv)
# PRINT LISTS HERE
for x in range(countinv):
print("\n", invlist[x])
# for x in range(countinv):
# print(invlist[x])
# START - Welcome
clear()
print("Hi! Welcome to Python Inventory System.")
skip()
clear()
# START - Introduction
print("This is an Inventory System where you can input any Inventories you want and how many you want.")
print("For e.g.: You can input 3 types of Inventories such as Vegetables, Fast Foods, Drinks, etc.")
print("And you can input the prices for each item.")
skip()
clear()
# COMMENCE PROGRAM
x = 0
while x != 1:
start = input("Are you ready to Start? (Y/N):")
if start == 'y' or start == 'Y':
x += 1
createINV()
elif start == 'n' or start == 'N':
x += 1
clear()
else:
x = 0
【问题讨论】:
标签: python-3.x list dictionary for-loop input