'Python Code Error: I can't add more than one "product" to my app
I have this supplyChain demo app, made it for learning. I have a problem in it though. I cannot add more than one "product", "chain", or "type/destination". What can I do to fix it?
Here it the code:
print("Welcome to supplyManage by PCMX ./Code\nType in .{help} to get started with commands")
initcmd = input("./: ")
if initcmd == ".{help}":
print(
"Welcome to supplyManage\n"
"Start by creating chain: ./create{}\n"
"Add product to chain by ./chain/pro\n"
"Add chain type by: ./chain/type\n"
"Add destination by: ./chain/desti\n"
"Type ./chain/log for all lists printed\n"
"As of now, only one chain can be made per program"
"Now rerun program to load\n"
)
if initcmd == "./create{}":
ask = input("Name of Chain: ")
chain = ask
chainpro = [] # List of products
chaintype = [] # List of Types
destichain = [] # final place of products
print("Chain {} is made.".format(chain))
initcmd = input("./: ")
if initcmd == "./{}/pro".format(chain):
productname = input("Name of Product: ")
chainpro.append(productname)
initcmd = input("./: ")
if initcmd == "./{}/type".format(chain):
typepro = input("Shipping Type: ")
chaintype.append(chaintype)
initcmd = input("./: ")
if initcmd == "./{}/desti".format(chain):
prodesti = input("Final Place of Product: ")
destichain.append(prodesti)
initcmd = input("./: ")
if initcmd == "./{}/log".format(chain):
asklogprint = input("We are going to export as a txt file. y or n")
if asklogprint == "n":
initcmd = input("./: ")
elif asklogprint == 'y':
import json
with open("DataProducts.txt", 'w') as savepro:
savepro.write(json.dumps(chainpro, chaintype, destichain))
initcmd = input("./: ")
else:
print("Invalid Response")
inicmd = input("./: ")
Solution 1:[1]
You are going to want to nest everything in a while loop and have that act as a switch to keep the program running, and use similar logic to allow for multiple inputs at once.
For multiple inputs:
x = 0
arr = []
while x != 'done':
x = input('enter a number: \n')
for i in x:
if x != 'done':
arr.append(x)
This code above allows consecutive inputs until the user types 'done', and appends values to an array. As rayryeng pointed out, you also want to nest everything a similar while loop in order to keep the program running.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jonathan Fields |
