Results of for loop not adding to variable - python-2.7

So i made the following code to calculate my overal adverage mark:
def report_card(GPA):
total = 0
for courses, marks in GPA:
print courses,'-',marks
for courses, marks in GPA:
total = total =+ marks
print total / len(GPA)
report_card([('History', 84), ('Physics', 76), ('English', 91), ('Science', 64)])
I thouht since I want total to be total += marks it would count count up all the marks of the subject i follow up together, but this is not happening, it prints the marks one-by-one and divide this one mark by 4 (the length of GPA)
So how do i make my marks be counted up as one number

The syntax is either:
total = total + marks
or:
total += marks
What you wrote is parsed as if you'd written:
total = total = (+marks)
+marks is equivalent to just marks, so it's equivalent to:
total = total = marks
which is equivalent to:
total = marks

Related

multiple conditions on multiple variables

would really love to get some help on a sas program i am trying to write
I have five variables that have the options Interest or Rewards
call them wo1 wo2 wo3 wo4 wo5
is there a way to do an if statement that checks all five for the value of "Interest" and then produces a calcualted variable with the totals of the variable that holds the amount value which are wo1amt wo2amt wo3mt etc.
and the ones that do not have the value of interest return a value of 0.
or would i need to do an if statement for each, creating a calc var for each and then summing them all together with another ??
any assistance, direction would be very appreciated...
If I'm understanding correctly, it looks like you will need multiple if statements.
You could initialize a total variable, and then add to it for each contributing variable, like this:
total = 0;
if wo1 = 'Interest' then total = total + wo1amt;
if wo2 = 'Interest' then total = total + wo2amt;
if wo3 = 'Interest' then total = total + wo3amt;
if wo4 = 'Interest' then total = total + wo4amt;
if wo5 = 'Interest' then total = total + wo5amt;
Arrays work nicely for situations like this.
array wo(*) wo1-wo5;
array woamt(*) wo1amt wo2amt wo3amt wo4amt wo5amt;
total = 0;
do i = 1 to 5;
total + woamt(i)*(wo(i)='Interest');
end;

Invalid Syntax attempting to write Python closure

I am attempting to write a function (in Python 2.7) which takes an outstanding balance and annual interest rate then returns the min monthly payment to the nearest cent using bisection search to solve problem #3. I am trying to follow DRY principles by writing a function inside the main function which should return a list with the balance after a year and the number of months (the loop should break if balance hits zero or less) which will need to be calculated twice in my main function. As I try to test this initial closure before moving on I am getting a syntax error on the line assigning monthlyPayment. What am I doing wrong?
# Problem Set 1("C")
# Time Spent: xx hours
def payInOne_BisectionSearch (balance,annualRate):
#initialize variables
initialBalance = balance
monthlyRate = annualRate/12
minMonthly = balance/12
maxMonthly = (balance * (1 + monthlyRate ** 12 )/12
monthlyPayment = (minMonthly + maxMonthly)/2
numMonths = 1
#define function to check balance after 12 months
def balanceAfterYear (balance, monthlyRate, monthlyPayment):
for numMonths in range (1,13):
interest = balance * monthlyRate
balance += interest - monthlyPayment
if balance <= 0:
break
return [balance, numMonths]
resultList = balanceAfterYear(initialBalance, monthlyRate, monthlyPayment)
print resultList[0],resultList[1]
payInOne_BisectionSearch (input("Enter the outstanding balance"),input("Enter annual rate as a decimal"))
You forgot a closing bracket in the previous line.
maxMonthly = (balance * (1 + monthlyRate ** 12 )/12

Multiplying the list of numbers without multiply

I am trying to multiply the elements in the list so that they give me their total but with only using addition and subtraction. For example, a list of [1,3,6,8] will have the output 144. The code I have so far is:
numbers = [1,3,6,8]
def no_sign(numbers):
total = 0
answer = 0
for i in range(len(numbers)):
first_number = numbers[i]
print str(first_number) + ' pop'
for j in range(first_number):
#print first_number
answer = first_number + answer
print str(first_number) + ' firstnum'
print str(answer)+ " answeer "
total = total + answer
print str(total) + " total"
return total
print no_sign(numbers)
This only gives me 110, which isn't enough. Any suggestions?
Your code takes the square of each element and adds them up. Hence you are getting 1 + 9 + 36 + 64 = 110
Since you want to do same thing couple times, writing your multiplication(num1, num2) function yourself with only addition and using that when multiplying would be much better choice.
Multiplication of two numbers, as you know, is adding firstNumber to itself secondNumber of times. S you can write multiplication function like below and use it on a list.
def multiplication(num1, num2):
answer = 0
for i in range(num2):
answer += num1
return answer
numbers = [1,3,6,8]
def no_sign(numbers):
total = 1
for number in numbers:
total = multiplication(total, number)
return total
print no_sign(numbers)

Why is my code getting an infinite loop?

minimunpaymonth = 0
balance = 4773
annualInterestRate = 0.2
def function(minimunpaymonth):
global balance
month = 1
while month <= 12:
balance = balance - minimunpaymonth
anninterest = annualInterestRate/12 * balance
balance = balance + anninterest
month += 1
return balance
while function(minimunpaymonth) >= 0:
minimunpaymonth += 10
print "Lowest Payment: " + str(minimunpaymonth)
the second while loop is infinite and i dont know why. the first is ok because i have ran it
when the loop increases minimunpaymonth, value of balance goes down, so there will be a moment when balance is negative
def function(minimunpaymonth, balance, annualInterestRate):
month = 1
while month <= 12:
balance = balance - minimunpaymonth
anninterest = annualInterestRate/12 * balance
balance = balance + anninterest
month += 1
return balance
while function(minimunpaymonth, balance, annualInterestRate) >= 0:
minimunpaymonth += 10
print "Lowest Payment: " + str(minimunpaymonth)
ok i just solved it. i change the function to give 3 arguments instead of 1
your second loop is checking to see if minimunpaymonth is >= 0, if it is then it performs the loop again.
Your minimunpaymonth will always be >=0 because it starts at 0 & is only ever added to. There is no subtraction from this value.
the second loop keeps adding to minimum payment, it will always be >= 0 till it reaches the numeric limit of the variable; however as the comment pointed out, the "function" could get less, but perhaps the interest rate always keeps the balance above zero and the minimum payments don't get it there -- plausible enough in real life!

In python, how to convert specific values in a list to different values based on special conditions

I am trying to write a program that converts a percentage received in a class into a GPA format then eventually find the overall GPA. I want it to first prompt the user to input the amount of classes, then input the percentages. I am running into some trouble getting the program to convert the percentage into the GPA format (90 or greater equals 4, 80 to 89 equals 3, ect...). This is what I have so far
class_number = int(raw_input("How many classes do you have? "))
total_grade = list((raw_input("Enter Percentage: ")) for i in range(class_number))
a = total_grade
def alter(x):
if x >= 90:
return 4
elif x >= 80:
return 3
a = map(alter,a)
print a
The problem is that this only seems to output 4s no matter the original percentage.
Any help would be greatly appreciated! Thanks!
That's because x is actually a string. I think a string will always be greater than a number. You need to convert each item in total_grade to an int:
total_grade = list(int(raw_input("Enter Percentage: ")) for i in range(class_number))
Also, you can just use a list comprehension:
total_grade = [int(raw_input("Enter Percentage: ")) for i in range(class_number)]