I'm creating a simple shell program that accepts commands from the user for a restaurant. But I'm very confused on how to accept multiple inputs from the user so that the shell can read it and then interpret it to follow with the called function.
iinventory = {
"apple": 0,
"beets": 0,
"carrots": 0}
total=20
def handle_commands():
keep_going=True
while keep_going:
command=input("$ ").split()
var1=input()
var2=input()
if command == var1 + var2:
var1="loadrecipefile"
var2=recipe_file
loadrecipefile(recipe_file)
elif command == "printrecipes":
printrecipes()
elif command == "printiinventory":
printiinventory()
elif command == "printmoney":
printmoney()
elif command == "preparedish ":
command=input().split(" ")
preparedish(recipe_name)
elif command == "buyingredient ":
command=input().split(" ")
buyingredient(name, number)
elif command == "quit":
keep_going=False
break
else:
print("Sorry that is not an acceptable command")
return
def loadrecipefile (recipe_file):
infile=open(recipe_file)
Linelist=infile.readlines()
for line in Linelist:
wordList=line.split()
r1={'apple':int(wordList[1]),'beets':int(wordList[2]),'carrots':int(wordList[3])}
cookbook={wordList[0]:r1}
def printrecipes():
for name,ingred in cookbook.items():
print(name + " " + str(ingred['apple']) + " " + str(ingred['beets']) + " " + str(ingred['carrots']))
def buyingredient(name, number:int):
global total
total_spent=0
if number + total_spent > total:
print("Not enough cash!")
iinventory[name] += number
total -= number
def printiinventory():
print(iinventory['apple'],iinventory['beets'],iinventory['carrots'])
def printmoney():
print(total)
def CanPrepareDish(recipe_name):
recipe = cookbook[recipe_name]
for ingred in recipe:
if ingred not in iinventory:
return False
if iinventory[ingred] < recipe[ingred]:
return False
return True
def preparedish(recipe_name):
if not CanPrepareDish(recipe_name):
print("Not enough ingredients")
else:
recipe = cookbook[recipe_name]
for ingred in recipe:
iinventory[ingred] -= recipe[ingred]
if recipe_name in iinventory:
iinventory[recipe_name] +=1
else:
iinventory[recipe_name] = 1
print("Dish prepared")
handle_commands()
For example, if the user was to execute:
$ buyingredient apple 5
The program should be able to accept all 3 of the different inputs then split the inputs by the spaces and then execute the function, buyingredient. I don't know how to accept the multiple inputs in one line of command.
AND, for the printrecipes()
function, I want it to print the cookbook that is created in the loadrecipefile function but it says that the cookbook is not defined so how do I use the cookbook from the loadrecipefile function for the printrecipes()
. The cookbook is a dictionary and I've tried making the cookbook={}
outside of the function but printrecipes()
just prints it as blank rather than printing the cookbook that was created in loadrecipe file
def loadrecipefile (recipe_file):
infile=open(recipe_file)
Linelist=infile.readlines()
for line in Linelist:
wordList=line.split()
r1={'apple':int(wordList[1]),'beets':int(wordList[2]),'carrots':int(wordList[3])}
cookbook={wordList[0]:r1}
print(cookbook)
So for example, using the above code, it would print the cookbook:
loadrecipefile("recipe_file.txt")
{'Recipe1': {'apple': 1, 'beets': 4, 'carrots': 3}}
{'Recipe2': {'apple': 0, 'beets': 2, 'carrots': 4}}
{'Recipe3': {'apple': 3, 'beets': 0, 'carrots': 1}}
{'Recipe4': {'apple': 2, 'beets': 1, 'carrots': 0}}
and with printrecipes()
it should print
Recipe1 1 4 3 Recipe2 0 2 4 Recipe3 3 0 1 Recipe4 2 1 0
Any help would be appreciated.