# ... (previous code)
def dispense_item(counter, item_name, gate_duration, buzzer_freq, buzzer_duration):
if counter > 0:
try:
# Check if the input is a valid number
number = int(c)
# Dispensing logic
print(f"\nYOU HAVE ORDERED {item_name}!")
print(f"PLEASE WAIT {gate_duration} SECONDS\n")
for _ in range(1):
sleep(0.5)
gate.move(90)
sleep(0.5)
gate.move(0)
sleep(gate_duration)
buzzer.init(freq=buzzer_freq, duty=1023)
sleep(0.5)
buzzer.init(freq=1, duty=10)
sleep(0.5)
gate.move(90)
sleep(1)
print(f"{item_name} READY!")
print(f"HERE IS YOUR {item_name}")
print("THANK YOU AND COME AGAIN!\n")
counter -= 1
except ValueError:
print("Invalid input. Please enter a number.")
else:
print(f"{item_name} IS UNAVAILABLE!")
print("PLEASE ORDER SOMETHING ELSE\n")
return counter
while True: # to endless repeating
# ... (previous code)
c = input("\nEnter a value: ")
if c == "1":
counter10 = dispense_item(counter10, "NASI LEMAK", 5, 1000, 0.5)
if c == "2":
counter20 = dispense_item(counter20, "KUEY TEOW GORENG", 5, 1000, 0.5)
if c == "3":
counter30 = dispense_item(counter30, "CENDOL", 5, 1000, 0.5)
if c == "4":
counter40 = dispense_item(counter40, "ICED TEH TARIK", 3, 1000, 0.5)
# ... (remaining code)