## Beginner help; min and max

This is the place for queries that don't fit in any of the other categories.

### Beginner help; min and max

Hello there!

I am just about to start to use and learn python, and I am using the tutorials found on this website;

http://cscircles.cemc.uwaterloo.ca/

I am stuck on a problem found on

http://cscircles.cemc.uwaterloo.ca/2x-extra-practice/

called "payment calculator".

A credit card company computes a customer's "minimum payment" according to the following rule. The minimum payment is equal to either \$10 or 2.1% of the customer's balance, whichever is greater; but if this exceeds the balance, then the minimum payment is the balance. Write a program to print out the minimum payment using min and max. Assume that the variable balance contains the customer's balance. Your program does not need to print the dollar sign.
Example 1: if your balance is 1000, then your program should print 21.
Example 2: if your balance is 600, then your program should print 12.6.
Example 3: if your balance is 25, then your program should print 10.
Example 4: if your balance is 8, then your program should print 8.

I am, as stated, a complete beginner; I am conscious of how simple this probably looks to you! What I have so far, is this;

Balance = 1000, 600 or 25, that is 1-3 on the list, this works out
Code: Select all
`MinimumPayment = max(Balance * 0.021, 10)`

And, when it is 8, which refers to example 4;
Code: Select all
`MinimumPayment = min(Balance, 10)`

However, I`m not sure how to express this in the code, to get the right results; note that this problem is supposed to be solved only by using "min" and "max" functions!

I`d love to get some input on this!
Best regards, Bjorn.C
Last edited by stranac on Sun Feb 24, 2013 12:14 am, edited 1 time in total.
Reason: Added some formatting
Bjorn.C

Posts: 6
Joined: Sat Feb 23, 2013 11:33 pm

### Re: Beginner help; min and max

Break it down in to stages are you able to do the python code that calculates the minimum payment for starters?
Code: Select all
`balance = 1000minimumPayment = #your code hereprint(minimumPayment==21.0)`

fill in the 'Your code here' part of the above so when you run this its print true.
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

Yoriz

Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: Beginner help; min and max

It's simple really.
You have the two parts(almost) figured out.
Now you just need to combine them.

So, first get the bigger of 10 and 2.1%. Your code for this part is fine(except the variable name is balance, not Balance).
Then, just get the smaller one of that result and the current balance.

I'm trying to keep my help minimal and not give you a solution, in case you want to find it yourself.
If you decide you can't figure it out, let me know, and I'll show you some working code.
Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

### Re: Beginner help; min and max

Yoriz wrote:Break it down in to stages are you able to do the python code that calculates the minimum payment for starters?
Code: Select all
`balance = 1000minimumPayment = #your code hereprint (minimumPayment==21.0)`

fill in the 'Your code here' part of the above so when you run this its print true.

Hm ok! Would this be correct, given your example;

Code: Select all
`balance = 1000MinimumPayment = max(balance * 0.021, 10) print(MinimumPayment)`

Here, I get 21, which is the desired output in example 1.
My problem, is that I`m not sure how to proceed so as to get the right result for example 4 as well!
Bjorn.C

Posts: 6
Joined: Sat Feb 23, 2013 11:33 pm

### Re: Beginner help; min and max

OK that's good you jumped to next next stage i was going to give instead
now from what you have you need to use min to get the minimum of the result you just got and the given balance
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

Yoriz

Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: Beginner help; min and max

Bjorn.C wrote:
Yoriz wrote:Break it down in to stages are you able to do the python code that calculates the minimum payment for starters?
Code: Select all
`balance = 1000minimumPayment = #your code hereprint (minimumPayment==21.0)`

fill in the 'Your code here' part of the above so when you run this its print true.

Hm ok! Would this be correct, given your example;

Code: Select all
`balance = 1000MinimumPayment = max(balance * 0.021, 10) print(MinimumPayment)`

Here, I get 21, which is the desired output in example 1.
My problem, is that I`m not sure how to proceed so as to get the right result for example 4 as well!

hint: you need to apply a min function to the result of your max function.
Code: Select all
`<function signature at 0xb73f910c>`

Jaro

Posts: 8
Joined: Sat Feb 23, 2013 6:16 pm

### Re: Beginner help; min and max

Yoriz wrote:OK that's good you jumped to next next stage i was going to give instead
now from what you have you need to use min to get the minimum of the result you just got and the given balance

Hm, do you mean that I should get the minimum of;

balance = 1000
MinimumPayment = max(balance * 0.021, 10)
print(MinimumPayment)

which would be

min(max(balance * 0.021, 10), (balance))

The first being the result I got, and the "(balance)" being the given balance?

:O
Bjorn.C

Posts: 6
Joined: Sat Feb 23, 2013 11:33 pm

### Re: Beginner help; min and max

Bjorn.C wrote: min(max(balance * 0.021, 10), (balance))

Have you tried this? Does it work? If it does, then congratulations: you solved the problem.
Code: Select all
`<function signature at 0xb73f910c>`

Jaro

Posts: 8
Joined: Sat Feb 23, 2013 6:16 pm

### Re: Beginner help; min and max

Try your code in here it should print true 4 times
Code: Select all
`balances = ((1000, 21), (600, 12.6), (25, 10), (8, 8))for balance, result in balances:    minimumPayment = max(10, 0.021*balance)    minimumPayment = #your code here to get the minimum of minimumPayment  and balance    print(round(minimumPayment, 1)==result)`

replace #your code here to get the minimum of minimumPayment and balance

Edit: i was breaking it down into stages if you want to do it in one line try it in here
Code: Select all
`balances = ((1000, 21), (600, 12.6), (25, 10), (8, 8))for balance, result in balances:    minimumPayment = #your code here    print(round(minimumPayment, 1)==result)`

Note there is proof that using code tags is imported, until stranac put code tags around your python code in the first post i didn't think there was any i thought it was just pseudo code
Last edited by Yoriz on Sun Feb 24, 2013 12:51 am, edited 1 time in total.
Reason: Added note about code tags
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

Yoriz

Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: Beginner help; min and max

Yoriz wrote:Try your code in here it should print true 4 times
Code: Select all
`balances = ((1000, 21), (600, 12.6), (25, 10), (8, 8))for balance, result in balances:    minimumPayment = max(10, 0.021*balance)    minimumPayment = #your code here to get the minimum of minimumPayment  and balance    print(round(minimumPayment, 1)==result)`

replace #your code here to get the minimum of minimumPayment and balance

Edit: i was breaking it down into stages if you want to do it in one line try it in here
Code: Select all
`balances = ((1000, 21), (600, 12.6), (25, 10), (8, 8))for balance, result in balances:    minimumPayment = #your code here    print(round(minimumPayment, 1)==result)`

Yes, this gave "true", 4 times!
Also, when I modified the code like this;
Code: Select all
`balance = # one of the values listed in the examplesminimumPayment = max(10, 0.021*balance)minimumPayment = min(max(balance * 0.021, 10), (balance))print(minimumPayment)`

I got the right result every time! However, the tutorial still thinks that it fails; it says;

Did not pass tests. Please check details below and try again.
Results for test case 1 out of 5
Before running your code: We defined balance equal to 11.
Program executed without crashing.
Program output:
8
Expected this correct output:
10
Result of grading: Output line 1, value 8, did not match expected value 10"""

Haha, you helped me, and I got the right result, however, the site still thinks that I failed..
Bjorn.C

Posts: 6
Joined: Sat Feb 23, 2013 11:33 pm

### Re: Beginner help; min and max

Your code should be ok the answer is
Code: Select all
`print(min(max(balance * 0.021, 10), balance))`

You don't need the brackets around the balance in the 'min' part of the code but them being there won't cause a problem.
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

Yoriz

Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: Beginner help; min and max

Yoriz wrote:Your code should be ok the answer is
Code: Select all
`print(min(max(balance * 0.021, 10), balance))`

You don't need the brackets around the balance in the 'min' part of the code but them being there won't cause a problem.

Yes indeed! Thank you!

If you have the time, it would be interesting to see if you could solve it so that the tutorial produces the "Correct" after you have run the code; even though you gave me the right answer, it does not call "correct"...

Regardless, thank you all so much!
Bjorn.C

Posts: 6
Joined: Sat Feb 23, 2013 11:33 pm

### Re: Beginner help; min and max

You don't need to define balance. The site does it for you, for each test case.
So this works:
Code: Select all
`minimumPayment = max(10, 0.021*balance)minimumPayment = min(max(balance * 0.021, 10), (balance))print(minimumPayment)`
Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

### Re: Beginner help; min and max

Bjorn.C wrote:
Yoriz wrote:Your code should be ok the answer is
Code: Select all
`print(min(max(balance * 0.021, 10), balance))`

You don't need the brackets around the balance in the 'min' part of the code but them being there won't cause a problem.

Yes indeed! Thank you!

If you have the time, it would be interesting to see if you could solve it so that the tutorial produces the "Correct" after you have run the code; even though you gave me the right answer, it does not call "correct"...

Regardless, thank you all so much!

The tutorial does give 'correct'
Payment Calculator.jpg (84.75 KiB) Viewed 5287 times
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

Yoriz

Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: Beginner help; min and max

stranac wrote:You don't need to define balance. The site does it for you, for each test case.
So this works:
Code: Select all
`minimumPayment = max(10, 0.021*balance)minimumPayment = min(max(balance * 0.021, 10), (balance))print(minimumPayment)`

Ah, got it. I can see why that works now!
Thank you, again; I hope that you excuse me, if my questions have been redundant or stupid. Starting to understand these things now!

Good night, sir
Bjorn.C

Posts: 6
Joined: Sat Feb 23, 2013 11:33 pm

### Re: Beginner help; min and max

No of course they where not stupid, glad you got there in the end. It was just tricky giving help without just giving the answer.
Feel free to come back and ask some more stupid(only joking ) questions in the future.
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

Yoriz

Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Return to General Coding Help

### Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 5 guests