0

I have modified the remainder variable's placement in my code ( from the main function to the amount_due(coin, amount_due) function), which eliminated the Type error, and set up some pseudocode to keep me on track and at least figure out where did I go wrong. What I am trying to do is create a code that will prompt the user to enter coins one at a time with the accepted denominations (hence coins list), and show the remainder amount due after they've entered a coin until the total price value has been met. This is what was assigned:

Suppose that a machine sells bottles of Coca-Cola (Coke) for 50 cents and only accepts coins in these denominations: 25 cents, 10 cents, and 5 cents.

In a file called coke.py, implement a program that prompts the user to insert a coin, one at a time, each time informing the user of the amount due. Once the user has inputted at least 50 cents, output how many cents in change the user is owed. Assume that the user will only input integers and ignore any integer that isn’t an accepted denomination.

coins = [25,10,5]

def main():
    amount_due = 50
    print(f"Amount Due: {amount_due}")
    coin = evaluated(amount_due)


def evaluated(a):
    while True:
        coin = int(input("Insert Coin:").strip())
        if coin != coins:
            print(f"Please enter your coins in the following increments: {coins}")
            return coin
        elif coin == coins:
            continue

def amount_due(coin, amount_due):
   remainder = amount_due-coin
   if remainder <= 0:
        print(f"Change owed: {remainder}")

   if remainder > 0:
        print(f"Amount due: {remainder}")
        return remainder

main()

My UPDATED code as requested (just removed the != and replaced it with not in):

coins = [25,10,5]

def main():
    amount_due = 50
    print(f"Amount Due: {amount_due}")
    coin = evaluated(amount_due)


def evaluated(a):
    while True:
        coin = int(input("Insert Coin:").strip())
        if coin not in coins:
            print(f"Please enter your coins in the following increments: {coins}")
            return coin

        elif coin in coins:
            continue


def amount_due(coin, amount_due):
   remainder = amount_due-coin
   if remainder <= 0:
        print(f"Change owed: {remainder}")

   if remainder > 0:
        print(f"Amount due: {remainder}")
        return remainder

main()

UpAndAdam
  • 4,515
  • 3
  • 28
  • 46
Mia
  • 1
  • 2
  • 2
    Welcome to stack overflow! Please [edit] your question to include your code as text in the body of the question not as an image or link, to make a [mcve] – G. Anderson Aug 02 '23 at 16:35
  • Note that `if coin != coins:` will always evaluate to `True`, because an `int` will never equal a `list`. Perhaps you mean to use `if coin not in coins` or similar instead? – G. Anderson Aug 02 '23 at 16:36
  • Thank you! I will try that instead and see if that works. – Mia Aug 02 '23 at 16:52
  • okay, so now that I have edited the if coin not in coins part and the issue i'm running into now is that once the value is inputted when prompted Insert Coin: prompts again. Not sure if I missed something with my conditions. – Mia Aug 02 '23 at 17:14
  • please put your updated code DIRECTLY in the question thank you – UpAndAdam Aug 02 '23 at 17:39
  • your code doesnt make sense, why are you returning when they input a bad coin and doing nothing but continuing when they enter a good coin? amount_due is never checked... if they enter a good coin you need to pass it to a call to amount_due and then go around the loop again.. than you have it loop while what amount_due returns is greater than 0 – UpAndAdam Aug 02 '23 at 17:43
  • Based on asking new questions each time one is resolved, it seems like you've coded a lot without checking/testing as you go. Have a look at [How to step through python code to debug issues](https://stackoverflow.com/questions/4929251/how-to-step-through-python-code-to-help-debug-issues) and perhaps even start your code from scratch, and before coding each step, decide: What is my input? What should my output be? Then, after coding: Does my output match my expectation? – G. Anderson Aug 02 '23 at 19:56
  • @G.Anderson and UpAndAdam thank you all for your help it is very useful! Thanks a million! – Mia Aug 02 '23 at 21:30

1 Answers1

0

I changed some variable names to make it more clear and avoid having a function and a variable share the same name which is just bad juju. Notice how I make a call into the process function from the pay function


def main():
    total_cost = 50
    print(f"Amount Due: {total_cost}")
    pay(total_cost)


def pay(remainder):
    coins = [25,10,5]

    while remainder > 0:
        coin = int(input("Insert Coin:").strip())
        if coin not in coins:
            print(f"Please enter your coins in the following increments: {coins}")
        elif coin in coins:
            remainder = process(coin, remainder)


def process(coin, remaining_cost):
   remainder = remaining_cost - coin
   if remainder <= 0:
        print(f"Change owed: {remainder}")
   if remainder > 0:
        print(f"Amount due: {remainder}")
   return remainder


main()
UpAndAdam
  • 4,515
  • 3
  • 28
  • 46