Hi, i was working on a code for a vending machine and i do consider myself to be a beginner, anyway i feel my code is much longer than it needs to be and i want someone to show me a new, shorter way of presenting it.

import time
print ("Welcome to the Python Vending Machine.")

# Asking the user how much money they wish to enter.
number_of_10p = int(input("How many 10 pence coins would you like to insert? "))
while number_of_10p < 0:
    number_of_10p = int(input("Please enter a positive number."))

number_of_20p = int(input("How many 20 pence coins would you like to insert? "))
while number_of_20p < 0:
    number_of_20p = int(input("Please enter a positive number."))

number_of_50p = int(input("How many 50 pence coins would you like to insert? "))
while number_of_50p < 0:
    number_of_50p = int(input("Please enter a positive number."))

number_of_100p = int(input("How many 1 pound coins would you like to insert? "))
while number_of_100p < 0:
    number_of_100p = int(input("Please enter a positive number."))

# Creating a variable to store the total amount of money inserted into the vending machine.
change = round(((number_of_10p * 0.10) + (number_of_20p * 0.20) + (number_of_50p * 0.50) + (number_of_100p * 1.00)),2)

# Informing the user how much they have entered in total.
print ("\nIn total you have entered £", change)
time.sleep(2)
# Creating variables for the 5 products and their respective prices. 
product_1, product_1_price = "Flake", 0.55
product_2, product_2_price = "Wispa", 0.50
product_3, product_3_price = "Crunchie", 0.65
product_4, product_4_price = "Milky Way", 0.35
product_5, product_5_price = "Boost", 0.65

# Creating variables to track the number of each items bought,
flakes_bought = 0
wispas_bought = 0
crunchies_bought = 0
milky_ways_bought = 0
boosts_bought = 0

# Informing the user of the choices available and the prices that of each item.
print ("There are 5 products available to pick from;\n")
time.sleep(2)
print ("Item: {}, Price {} ".format(product_1, product_1_price))
print ("Item: {}, Price {} ".format(product_2, product_2_price))
print ("Item: {}, Price {} ".format(product_3, product_3_price))
print ("Item: {}, Price {} ".format(product_4, product_4_price))
print ("Item: {}, Price {} ".format(product_5, product_5_price))
print ("")

# Asking the user to make a selection.
while change > 0:
    customer_choice = input("What would you like to buy? Type N when you are finished \n")
    if customer_choice == "Flake" or customer_choice == "flake" and change >= product_1_price:
        print ("You have chosen a", product_1, "these cost", product_1_price, "each,")
        change = round((change - product_1_price),2)
        flakes_bought = (flakes_bought + 1)
        print ("You have this much money remaining: £", change)

    elif customer_choice == "Wispa" or customer_choice == "wispa" and change >= product_2_price:
        print ("You have chosen a", product_2, "these cost", product_2_price, "each,")
        change = round((change - product_2_price),2)
        wispas_bought = (wispas_bought + 1)
        print ("You have this much money remaining: £", change)

    elif customer_choice == "Crunchie" or customer_choice == "crunchie" and change >= product_3_price:
        print ("You have chosen a", product_3, "these cost", product_3_price, "each,")
        change = round((change - product_3_price),2)
        crunchies_bought = (crunchies_bought + 1)
        print ("You have this much money remaining: £", change)

    elif customer_choice == "Milky Way" or customer_choice == "milky way" and change >= product_4_price:
        print ("You have chosen a", product_4, "these cost", product_4_price, "each,")
        change = round((change - product_4_price),2)
        milky_ways_bought = (milky_ways_bought + 1)
        print ("You have this much money remaining: £", change)

    elif customer_choice == "Boost" or customer_choice == "boost" and change >= product_5_price:
        print ("You have chosen a", product_5, "these cost", product_5_price, "each,")
        change = round((change - product_5_price),2)
        boosts_bought = (boosts_bought + 1)
        print ("You have this much money remaining: £", change)

    elif customer_choice == "N" or customer_choice == "n":
        print ("\nHere is your transaction details:\n")
        print ("You purchased: ")
        print (product_1, "x", flakes_bought)
        print (product_2, "x", wispas_bought)
        print (product_3, "x", crunchies_bought)
        print (product_4, "x", milky_ways_bought)
        print (product_5, "x", boosts_bought)
        print ("You have £", change, "remaining.")
        break

    elif change <= 0:
        print ("You have run out of money.")
        print ("\nHere is your transaction details:\n")
        print ("You purchased: ")
        print (product_1, "x", flakes_bought)
        print (product_2, "x", wispas_bought)
        print (product_3, "x", crunchies_bought)
        print (product_4, "x", milky_ways_bought)
        print (product_5, "x", boosts_bought)
        break

    else:
        print ("There has been an error or you may not have enough credit.")

Thanks.

Before making any suggestions, I would like to ask if your course of study (whether personal or formal) has gotten to functions and how to write them yet. This would be a big factor in how you approach thsis matter.

Hi :) i'm doing this for personal pleasure and i do understand all the basics of python, i just want to improve my understanding and, of course, a shorter code is a better code, i just want to understand how i can make it shorter and less repetitive. thanks.

The first thing is to never have variable names containing a variable index, such as product_1, product_2, etc. Use lists or tuples instead

pairs = [
    ("Flake", 0.55), ("Wispa", 0.50),("Crunchie", 0.65),
    ("Milky Way", 0.35), ("Boost", 0.65)
]
product, product_price = zip(*pairs)

Then product[0] is 'Flake',product[4] is 'Boost' and similarly for product_price.

You can write loops

for i, p in enumerate(product):
    print('Item: {} Price: {}'.format(p, product_price[i]))
commented: Thanks man, this really helped. +0

Write a program for a ticket machine for an urban railway (such as the Hanoi metro/skytrain thing they are building outside the school)
There are 12 stations on the line.
Passengers can get on at any station - ask them their starting station and their destination.
Calculate and print the price: this is calculated by charging 2,000VND per station.
For example: If a passenger gets on at station 3 and goes to station 6, their ticket should cost 6,000 dong.
Take their money and check to see if it is enough.
If there is enough money inserted, give them a ticket: the ticket should show the stations it is valid from and to and the price paid. Think about the type of loop to use here!
The machine should then be ready to sell the next ticket.
You don't need to give change :)
The machine should store the total money collected in a variable and print this out when the special code 1173 is entered as the destination station.
Help me with this please if you help me I will kiss you :)))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.