Question: I need help in writing a python program. You are making a script to purchase a soda. Your user can enter 5 for a nickel,
I need help in writing a python program.
You are making a script to purchase a soda. Your user can enter 5 for a nickel, 10 for a dime, and 25 for a quarter (Explain these choices to them). For simplicity we'll assume that they won't enter any other number besides 5, 10 or 25.
The base price of a soda is $1.00, but we'll set a random price prior to each soda purchase. We will vary the price by 0, 5, 10 or 15 cents in either direction from $1.00.
The base price for a soda is one dollar. We'll just make that 100 in our program. In Python we make names for constants in all caps. So make a descriptive variable name and set it's value to 100. That base price will never change, but if in the future we wanted to change the base price of all sodas we'd just have to change that variable value (rather than dig through the code and change the base price in several places).
In order to vary the price we want to import the random package. It has a method called randint that returns a random value between (and including) two endpoints. We want (-3,3). Make a variable to capture the price variance and set it to this randint method * 5. That will give us a value from the set (-15, -10, -5, 0, 5, 10, 15).
Now make another variable that equals the base price + that price variance. That's the price of the soda for this run of the script.
Ok! Let's print out the welcome text that also informs the user of the acceptable values. Now let's ask them what type of soda they would like and assign that to a variable.
Now print out something like "The current price of Diet Coke is 115 cents". Of course "Diet Coke" is a variable and so is 115. Remember that PRINT only prints strings, so you need to convert integers with str(). You'll also need to convert input to integers with int() to do math on user inputs.
Now we make a WHILE statement that runs forever (while True). Inside that we'll do the money thing. When it's over we'll print something like "Enjoy your Diet Coke" (where Diet Coke is a variable). So, inside the loop:
Get user input for the coin they enter and assign that to a variable. Use a good descriptive name. For this example I'll call that variable cat. Next line should add cat to a variable that I'll name dog here (you need a much better variable name). Dog is my running total of how much they have paid. I'm going to have to go back to the top of my script (just after I made that constant declaration), though, and say dog = 0. I can't do math to a variable that doesn't exist.
Next I'll make a variable to hold the balance (mouse) and say that mouse = the price of the soda - the running total of payments (dog).
Next is an IF/ELIF/ELSE statement. IF the balance is greater than zero I'll print out something like "You still owe 95 cents", where "95" is my variable mouse. Then Ill continue. ELIF the balance is less than zero I'll print out something like "You have been refunded 15 cents." where 15 is a variable. ELSE break. That should kick us out of the WHILE loop and into that "Enjoy your Diet Coke" type of print statement.
You may need abs() to get the absolute value of an integer.
abs(-15) == 15


Refund example: Welcome to the soda machine. You can enter values of 5, 10, or 25 in payment What type of sode would you like? Diet Coke The current price of Diet Coke is 110 cents. Insert a coin: 25 You still owe 85 cents. Insert a coin: 10 You still owe 75 cents. Insert a coin: 5 You still owe 70 cents. Insert a coin: 25 You still owe 45 cents. Insert a coin: 25 You still owe 20 cents. Insert a coin: 25 You have been refunded 5 cents. Enjoy your Diet Coke! Exact Payment example Welcome to the soda machine. You can enter values of 5, 10, or 25 in payment What type of sode would you like? Sprite The current price of Sprite is 110 cents. Insert a coin: 10 You still owe 100 cents. Insert a coin: 10 You still owe 90 cents. Insert a coin: 25 You still owe 65 cents. Insert a coin: 25 You still owe 40 cents. Insert a coin: 25 You still owe 15 cents. Insert a coin: 10 You still owe 5 cents. Insert a coin: 5 Enjoy your Sprite
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
