Reputation: 23
def main():
selectionvalid = False
print("Welcome to virtue vending machine!")
print("Machine only takes $1 bills")
table()
while True:
user = input("Select your choice:")
if user == 'A1':
selectionvalid = True
print("Snickers: $.89")
change = bills() - a1
elif user == 'A2':
print("M&Ms: $1.39")
change = bills() - a2
selectionvalid = True
elif user == 'A3':
print("M&Ms: $1.39")
change = bills() - a2
selectionvalid = True
elif user == 'B1':
print("Lays: $.50")
change = bills() - a3
selectionvalid = True
elif user == 'B2':
print("Doritos: $.50")
change = bills() - a3
selectionvalid = True
elif user == 'B3':
print("Cheetos: $1.49")
change = bills() - a5
selectionvalid = True
elif user == 'C1':
print("TicTac: $1.04")
change = bills() - a6
selectionvalid = True
elif user == 'C2':
selectionvalid = True
print("Starburst: $.79")
change = bills() - a7
elif user == 'C3':
selectionvalid = True
print("Skittles: $2.49")
change = bills() - a8
else:
print("Item not available.")
print("Please, select your choice again:")
if selectionvalid == True:
print("Your change is:$", format(change, '.2f'))
message()
break
return selectionvalid
def bills():
totals = 0
bills = int(input("Insert your bills:$"))
while bills > 4:
print("Please, enter less than $4")
bills = int(input("Insert your bills:$"))
else:
print("You've inserted $",bills,"dollars")
return bills
def message():
print("Your,",,"is dispensed below!")
print("Thank you! Come Again!")
I'm trying to get the user input selection to show up to def message() Is there another way to condense the code from the if-elif-else to something more like a loop? I've heard about dictionary and lists, but that seems too complicated for this I think. For example: "Your 'snicker' is dispensed below" "You 'Kitkat' is dispensed below"
Thanks
Upvotes: 0
Views: 98
Reputation: 897
How's a dictionary more complicated than what you already have? A dictionary will clean up a lot of your code and get ride of 1/3 of the lines, mostly the elif
s
vendingdict = {"A1": ["Snickers", 0.89],
"A2": ["M&Ms", 1.39],
"A3": ["Lays", 0.50]}
def main():
global myselection
while True:
myselection=input("Welcome to virtue vending machine!\nMachine only takes $1 bills\nSelect your choice:\n")
try:
if vendingdict[myselection] != None:
break
except KeyError:
print("\nPlease input valid selection\n")
continue
def bills():
while True:
try:
cashin = int(input("Insert your bills: $"))
except ValueError:
print("Please insert valid tender!")
continue
cashout = 0
if cashin>=vendingdict[myselection][1]:
cashout = cashin - vendingdict[myselection][1]
print("Your change is $%.2f" % cashout)
print("Your %s is below!" % vendingdict[myselection][0])
break
else:
print("Insufficient money, please insert more.")
print(cashin)
cashin = 0
continue
main()
bills()
Upvotes: 1
Reputation: 1779
Why can't you just send the user input selection to message()
as a parameter? Maybe create a dictionary products
with user inputs as keys and products as values. Then message(products[userInput])
.
Upvotes: 0