Hi all,

I'm new to this forum and I am also new to Python and coding in general. While I've gotten through basic tutorials on most general topics of Python such as series, dictionary, parameters, loops, and so forth I am still very much a newbie (as you will see in this program I attempted)

My boss at work wanted me to solve a simple math problem in order to help her create a new budget based on a recent grant our organization received. I eventually just did it the basic math way, however I wanted to test out my Python abilities so I tried to make a program to solve it.

Can someone please help me figure out how to get this code to work?

```
##Solve situation when: one fixed, one variable, one total of fixed + variable
##The OtherSub must be 75% of Total, the Contribution must stay fixed
##The Total can increase but not decrease
Contribution = input('Enter the fixed amount: ')
Other = input('Enter the other variable amount: ')
Total = Contribution + Other
##Putting a = 3. allows for .75 whereas simply 3/4 would give 0
a = 3.
b = 4
c = a / b
While Other / Total <= c:
print Other
print Other / Total
OtherSub + 1
If OtherSub / Total == c:
print 'The new Other Subtotal is ' + Other
```

Well, there are two issues

- You can just do this with introductory level algebra, and you should.
- You have a variable
`OtherSub`

that is never initialized, and even if it were, you should be saying`OtherSub += 1`

at line 17 (I think you meant)

For option 1, If I understand correctly, there are only two terms: Fixed and Variable. You can say

```
Variable == .75 * (Fixed + Variable)
=> Variable == .75*Fixed + .75*Variable # distribute the multiplication
=> .25 Variable == .75 Fixed # subtract .75*Variable from both sides
=> Variable == 3*Fixed # multiply both sides by 4
```

I would skip option 2.

Hi all,

I'm new to this forum and I am also new to Python and coding in general. While I've gotten through basic tutorials on most general topics of Python such as series, dictionary, parameters, loops, and so forth I am still very much a newbie (as you will see in this program I attempted)

My boss at work wanted me to solve a simple math problem in order to help her create a new budget based on a recent grant our organization received. I eventually just did it the basic math way, however I wanted to test out my Python abilities so I tried to make a program to solve it.

Can someone please help me figure out how to get this code to work?

`##Solve situation when: one fixed, one variable, one total of fixed + variable ##The OtherSub must be 75% of Total, the Contribution must stay fixed ##The Total can increase but not decrease Contribution = input('Enter the fixed amount: ') Other = input('Enter the other variable amount: ') Total = Contribution + Other ##Putting a = 3. allows for .75 whereas simply 3/4 would give 0 a = 3. b = 4 c = a / b While Other / Total <= c: print Other print Other / Total OtherSub + 1 If OtherSub / Total == c: print 'The new Other Subtotal is ' + Other`

I agree with griswolf that your statement of the problem is unclear, it seems that you give different names for the same quantities: fixed, variable, total, contribution, othersub. Please tell us how many values there are and the relations between them. I understand that

```
total = fixed + variable
othersub = 0.75 * total
```

but is it true that othersub = variable ?

Oh I'm sorry, looking at it again it is a mess. Maybe this renamed version is clearer.

##Solve situation when: one fixed, one variable, one total of fixed + variable

##The OtherCosts must be 75% of Total, the Contribution must stay fixed

##The Total can increase but not decreaseContributionCosts = input('Enter the fixed amount: ')

OtherCosts = input('Enter the other variable amount: ')

Total = ContributionCosts + OtherCostsWhile OtherCosts / Total <= .75:

print OtherCosts

print OtherCosts / Total

OtherCosts += 1

If OtherCosts / Total == .75:

print 'The new subtotal of the other costs is ' + OtherCosts

Basically the situation would be where I know the fixed cost (the contribution cost) lets say is $17,800. However, the other costs (the one allowed to change, it is more than one cost but since I'm using the total of those costs it is only one number) are a total of $24,420 which is 58% of the Full Total cost $42,220. I need to have the "Other Costs" be 75% of the Full Total. Since this situation sometimes comes in other forms I wanted to make a program that can deal with it.

The method I was trying to emulate was the crude method where you keep adding one to the "Other Costs" and double checking to see if it 75% of the Full Total Costs and if not, then adding one again and so forth until the "Other Costs" is 75%. I'm sure there are other methods but I chose this method because I wanted to practice looping or iterations.

Oh I'm sorry, looking at it again it is a mess. Maybe this renamed version is clearer.

Basically the situation would be where I know the fixed cost (the contribution cost) lets say is $17,800. However, the other costs (the one allowed to change, it is more than one cost but since I'm using the total of those costs it is only one number) are a total of $24,420 which is 58% of the Full Total cost $42,220. I need to have the "Other Costs" be 75% of the Full Total. Since this situation sometimes comes in other forms I wanted to make a program that can deal with it.

The method I was trying to emulate was the crude method where you keep adding one to the "Other Costs" and double checking to see if it 75% of the Full Total Costs and if not, then adding one again and so forth until the "Other Costs" is 75%. I'm sure there are other methods but I chose this method because I wanted to practice looping or iterations.

Yes, you are trying to write your own 'equation solver' or 'root finding' algorithm for the equation `other == proportion * (fixed + other)`

where the unknown is 'other'. Iterative methods to solve algebraic equations exist, but as griswolf said, this equation is elementary and can be solved immediately, so the best program would be

```
fixed = float(raw_input('Enter the fixed amount: $ '))
proportion = float(raw_input('Enter the proportion of variable costs: '))
other = proportion * fixed/(1.0 - proportion)
print "The variable costs must be $ %.2f." % other
""" my output --->
Enter the fixed amount: $ 17800
Enter the proportion of variable costs: .75
The variable costs must be $ 53400.00.
"""
```

Since you are saying that the situation sometimes comes in other forms, it means that you may have more complicated equations to solve. Iterative methods are typically needed for **non linear** equations.

Also notice that keywords like 'if' or 'while' are all in lowercase letters in python.

Oh wow that works perfectly. But what is float? I learned about raw_input for strings and input for integers but I don't know what float is used for.

And I don't understand what happened in this part:

print "The variable costs must be $ %.2f." % other

Where did .2f. come from?

input takes every kind of Python expression and evaluates them. It should not be used and therefore it is removed from Python3.

Format %.2f means: put here the variable after string and % with two fixed decimals format (like natural for currency).

So I finally fixed it the long looping way that I wanted to. The reason I didn't do it via basic algebra is because I am practicing looping. It wasn't about being efficient, because if it was I wouldn't have had to make this program in the first place.

so this is the new code:

```
##Solve situation when: one fixed, one variable, one total of fixed + variable
##The OtherCosts must be 75% of Total, the Contribution must stay fixed
##The Total can increase but not decrease
ContributionCosts = float(raw_input('Enter the fixed amount: '))
OtherCosts = float(raw_input('Enter the other variable amount: '))
Total = ContributionCosts + OtherCosts
NewOther = Total * .75
while OtherCosts / Total <= .75:
print OtherCosts
print OtherCosts / Total
print ' '
OtherCosts += 1
if OtherCosts / Total == .75:
print 'The new subtotal of other costs must be $ %.2f.' % OtherCosts
print ' '
print "Contribution costs: ",ContributionCosts
print "Total (Contribution costs + Other costs): ",Total
print "Alternative answer: ", NewOther
```

It seems to work fine. However, I noticed that until I did <=.75 the answer would stop at .74113241351351514 or something like that. Intuitively, having OtherCosts/Total <= .75 seemed like it would give me an answer slightly above .75 since even if OtherCosts/Total was at .75 the loop would tell me to add one more. However it didn't. Is this because I have OtherCosts += 1? The += 1 prevented that from happening?

Update - actually just realized it gives the wrong answer. gave me 53100 despite the check saying it was still .75 of total costs when fixed cost was 17800. This is wrong since 17800+53100 = 70900. 53100/70900 = .7489

When I start from even lower numbers it stops way earlier saying that it still is .75

You should learn to use floating point comparison by delta and absolute value difference, even the basic math version is correct one.

```
##Solve situation when: one fixed, one variable
contribution_costs = float(raw_input('Enter the fixed amount: '))
other_costs = 0
# criteria for near equality for floating point values
delta = 0.000001
increment = 0.01
percentage = 75
while not other_costs or abs(other_costs / total - percentage / 100.0) > delta:
other_costs += increment
total = contribution_costs + other_costs
if abs(other_costs / total - percentage / 100.0) < delta * 10:
print other_costs, other_costs / total
print("""
Contribution costs: $ %.2f
Other costs: $ %.2f
Total: $ %.2f""" % (contribution_costs, other_costs, total))
print("\n(Exact other costs: $ %.2f)" % (contribution_costs / (100.0 - percentage) * percentage))
```

Or here other way with generalized percentage.

```
##Solve situation when: one fixed, one variable
contribution_costs = float(raw_input('Enter the fixed amount: '))
increment = 0.01
percentage = float(raw_input('Enter the percentage of other costs: '))
other_costs = 0 if percentage > 50 else contribution_costs
# need total defined for 50/50 case
total = contribution_costs + other_costs
while not other_costs or other_costs / total < percentage / 100.0:
other_costs += increment
total = contribution_costs + other_costs
if 0 < other_costs / total * 100 - percentage < 0.1:
print other_costs, other_costs / total
other_costs -= increment
print("""
Contribution costs: $ %.2f
Other costs: $ %.2f
Total: $ %.2f""" % (contribution_costs, other_costs, total))
print("\n(Exact other costs: $ %.2f)" % (contribution_costs / (100.0 - percentage) * percentage))
```