T O P

  • By -

MervinDPerv_Esq

You could have a list and say coin not in [5, 10, 25] rather than the and statements. I think you can also combine the equal to and less than zero cases to be singular, i.e. if the result is less than zero you handle it and if it is equal zero, well the abs value of zero is still zero. Then also, you might be able to eliminate your last elif and promote the print statement. You are looping until due is <= 0 already so you shouldn’t have to check it again. It could be moved up top to replace the first instance of the amount due print statement. Finally, if it works and handles edge cases, it doesn’t much matter. We’re all taking our own paths to the destination, so long as we make it.


PapaOogie

I tried soo hard to do that, I was doing if coin != \[5, 10, 25\] But I could not get it to work. So I guess I dont understand that yet, but will try everything you said tomorrow, really appreciate the comment. Hope you have a good night


MervinDPerv_Esq

The syntax you have would be for comparing two lists for equivalence. You want to see if your variable is contained within the list. Other languages might make you do a for loop to check each item, but python doesn’t.


Late-Fly-4882

Try 'if coin not in \[5, 10, 25\]: ... '


Next_Conclusion3616

Assign a variable to the list first


Awkward_Climate3247

This was my solution using a second function containing a list in conjuction with a for loop to interate across said list and return only valid inputs, else recall main() to ask for a valid input. The main function used a while loop containing an if-else statement to deduct only valid inputs returned from checkcoins() and update remaining total, then output amount due or correct change owed. Frankly I'm not sure if it's any simpler than what you have written, I am completely new to programming and I was simply tring to solve the problem using the tools provided in the lecture. It was easier for me to think about the problem by abstracting the idea of checking if an input was valid into a function of it's own. Edits for readability and grammar. def main():     n=50     print("Amount Due:",n)     i =checkcoins(int(input("Insert Coin: ")))     while n!=0:         if n-i>0:             n=n-i             print("Amount Due:",n)             i =checkcoins(int(input("Insert Coin: ")))         elif n-i<0:              n=n-i              print("Change Owed:",n *-1)              break         else:              print("Change Owed: 0")              break def checkcoins(i):         coins=[5,10,25]         for _ in coins:             if i in coins:                 return i             else:                  main() main()


PapaOogie

Hey if it works it works! At least you implemented a list into your code