<type 'NoneType'> issue when doing math - python

I have the following code, but when i run it I get type 'NoneType'. I pinpointed to my "summation" variable being the one that has Nonetype, why is that? and how can i change it? I tried making into a float() but it did not work for me. Any insights would be welcome.
grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]
def grades_sum(scores):
total = 0
for item in scores:
total = item + total
print total
grades_sum(grades)
def grade_average(grades):
summation = grades_sum(grades)
average = summation / float(len(grades))
return average
print grade_average(grades)

Change the print to a return.
def grades_sum(scores):
total = 0
for item in scores:
total = item + total
return total

Well summation just calls the function, nothing else, so it is equal to a NoneType or basically nothing. Change print total to return total to allow summation to be equal to whatever total is.
def grades_sum(scores):
total = 0
for item in scores:
total = item + total
return total

When you define grades_sum(), you don't return anything, you just print it. Change it to this:
def grades_sum(scores):
total = 0
for item in scores:
total = item + total
print total
return total

Related

How to change this code to not printing unused coin=0?

I am trying to build a function that after people entering the amount of money, it will show the minimum number of coins or notes that they need. But this there any methods for me to change it so that it will not print the name and the number of the unused coin? (as a beginner) Thanks for helping! (Will it be possible to deal with it by using for loop?)
Instead of keeping a variable for every demonmation, keep a dict and update key: val based on the denominations used. See the code
amount=int(input('Enter an amount: '))
denominations = dict()
print('Total number of notes/coins=')
if amount>=1000:
denominations['1000'] = amount//1000
amount%=1000
if amount>=500:
denominations['500'] = amount//500
amount= amount%500
if amount>=100:
denominations['100'] = amount//100
amount= amount%100
if amount>=50:
denominations['50'] = amount//50
amount= amount%50
if amount>=20:
denominations['20'] = amount//20
amount= amount%20
if amount>=10:
denominations['10'] = amount//10
amount= amount%10
if amount>=5:
denominations['5'] = amount//5
amount= amount%5
if amount>=2:
denominations['2'] = amount//2
amount= amount%2
if amount>=1:
denominations['1'] = amount//1
for key, val in denominations.items():
print(f"{key}: {val}")
Enter an amount: 523
Total number of notes/coins=
500: 1
20: 1
2: 1
1: 1
You can reduce the number of lines of code if you use a simple logic like shown below,
def find_denominations():
amount=int(input('Enter an amount: '))
denominations = dict()
DENOMINATIONS = [1000, 500, 100, 50, 20, 10, 5, 2, 1]
print('Total number of notes/coins=')
for d in DENOMINATIONS:
if amount >= d:
denominations[d] = amount // d
amount %= d
for key, val in denominations.items():
print(f"{key}: {val}")
A similiar implementation to Sreerams using a while loop instead a for loop:
amount = int(input("Enter an amount: "))
counter = amount
pos = 0
notes = [1000, 500, 100, 50, 20, 10, 5, 2, 1]
output = []
while counter > 0:
remainder = counter % notes[pos]
sub = counter - remainder
num = int(sub / notes[pos])
counter -= sub
output.append({notes[pos]: num})
pos += 1
print("Total number of notes/coins=")
for r in output:
for k,v in r.items():
if v > 0:
print("{}: {}".format(k, v))
Please note Sreerams code is superior to mine, it's easier to read and would be more performant at scale.
Loop can be used iterate through a list of notes and inside the loop, if any note is found to be counted, that can be printed.
notes=[1000,500,100,50,20,10,5,2,1]
amount=int(input('Enter an amount: '))
print('Total number of notes/coins=')
for notesAmount in notes:
if amount>=notesAmount:
notesCount=amount//notesAmount
amount%=notesAmount
if notesCount>0:
print(notesAmount, ":", notesCount)

What does "real" in pseudocode indicate?

I'm trying to translate this pseudocode and can't do it accurately. In particular, I can't seem to figure out what real here means. This is the pseudocode:
Function Real average(Real values[], Integer size)
Declare Real total = 0.0
Declare Integer counter = 0
While counter < size
Set total = total + values[counter]
Set counter = counter + 1
End While
Return total / size
End Function
Declare Real scores[6] = 90, 80, 70, 100, 60, 80
Display average(scores, 6)
And this is what I've come up with:
def average(values[], int(size))
total = 0.0
counter = 0
while counter < size:
total = total + values[counter]
counter = counter + 1
return total / size
scores[6] = 90, 80, 70, 100, 60, 80
print(average(scores, 6))
Some languages use the term "real" in place of "float" etc. Therefore, in Python, with this bit of code you can leave it out.
..but there are a few things wrong with your code other than that. For example you just want
scores=[90,80, 70, 100, 60, 80]
then just give average "scores" and 6
Like this
def average(values ,size):
total = 0.0
counter = 0
while counter < size:
total = total + values[counter]
counter = counter + 1
return total / size
scores = [90, 80, 70, 100, 60, 80]
print(average(scores, 6))
Whilst clearly it is not necessary to do this in this way, I presume you are just learning Python...

Debug Change finding using Memoization

From the last couple of hours, I have been trying to debug this code.
Problem: Given a set of coins: 1, 5, 10, 21 and 25 write an algorithm that finds change for a given amount using the minimum possible number of coins.
Ex: if the amount is 63, it should return 3 [21, 21, 21]
My Code:
def change_rec_memo(change_list, amount, memo):
if amount in change_list:
memo[amount] = (1, [amount])
return 1, [amount]
if amount in memo:
return memo[amount]
mini, values, min_coin = None, [], None
for coin in change_list:
if amount - coin > 0:
count, sub_values = change_rec_memo(change_list, amount-coin, memo)
if mini is None:
mini = count
values = sub_values
min_coin = coin
if count < mini:
mini = count
values = sub_values
min_coin = coin
values.append(min_coin)
memo[amount] = (mini+1, values)
return mini+1, values
def main():
print change_rec_memo([1, 5, 10, 21, 25], 52, {})
The code seems to work fine for most of the cases, but fails for cases like 52, 63. It outputs the correct number of coins but the coin_listing contains extra coins.
Here is a Python Fiddle for it: https://pyfiddle.io/fiddle/08b3da45-0c0d-464a-b025-00d215bc4634/?i=true
there is one more readable and shorter example.and use stack variable(the parameter of recursive function)
import sys
#functools.lru_cache()
def change_rec_memo(change_list, amount, count):
if amount == 0:
return count
if len(change_list) == 0 or amount < 0:return sys.maxsize
if amount - change_list[-1] >= 0:
return min(change_rec_memo(change_list,amount - change_list[-1], count +1),change_rec_memo(change_list[:-1], amount, count))
else:
return change_rec_memo(change_list[:-1], amount, count)
print(change_rec_memo((1, 5, 10, 21, 25), 215,0))

Print the sum of a list of integers without using sum()

I have a function defined below that prints each integer in the list, and it works perfectly. What I would like to do is create a second function that would call on or reutilize the int_list() function to display a sum of the list that's been generated.
I am not sure if that has been inherently performed by the code itself - I am rather new to the Python syntax.
integer_list = [5, 10, 15, 20, 25, 30, 35, 40, 45]
def int_list(self):
for n in integer_list
index = 0
index += n
print index
In your code, you're setting index=0 in every loop, so it should be initialized before the for loop:
def int_list(grades): #list is passed to the function
summ = 0
for n in grades:
summ += n
print summ
output:
int_list([5, 10, 15, 20, 25, 30, 35, 40, 45])
5
15
30
50
75
105
140
180
225
To get the sum of a list of integers you have a few choices. Obviously the easiest way is sum, but I guess you want to learn how to do it yourself. Another way is to store the sum as you add it up:
def sumlist(alist):
"""Get the sum of a list of numbers."""
total = 0 # start with zero
for val in alist: # iterate over each value in the list
# (ignore the indices – you don't need 'em)
total += val # add val to the running total
return total # when you've exhausted the list, return the grand total
A third option is reduce, which is a function that itself takes a function and applies it to the running total and each consecutive argument.
def add(x,y):
"""Return the sum of x and y. (Actually this does the same thing as int.__add__)"""
print '--> %d + %d =>' % (x,y) # Illustrate what reduce is actually doing.
return x + y
total = reduce(add, [0,2,4,6,8,10,12])
--> 0 + 2 =>
--> 2 + 4 =>
--> 6 + 6 =>
--> 12 + 8 =>
--> 20 + 10 =>
--> 30 + 12 =>
print total
42
integer_list = [5, 10, 15, 20, 25, 30, 35, 40, 45] #this is your list
x=0 #in python count start with 0
for y in integer_list: #use for loop to get count
x+=y #start to count 5 to 45
print (x) #sum of the list
print ((x)/(len(integer_list))) #average
list = [5, 10, 15, 20, 25, 30, 35, 40, 45]
#counter
count = 0
total = 0
for number in list:
count += 1
total += number
#don'n need indent
print(total)
print(count)
# average
average = total / count
print(average)
You can used reduce function from functools module
from functools import module
s=reduce(lambda x,y:x+y, integer_list)
output
225

Not understanding why this won't sum up properly

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]
def grades_sum(grades):
sum = 0
for i in grades:
sum += grades[i]
print(grades_sum(grades))
That's my code and I'm trying to understand why I'm getting an out of index traceback.
Iterating over a list will return the item in the list, not the index of the item. The correct code as you have written it would look like this:
def grades_sum(grades):
total = 0
for grade in grades:
total += grade
return total
Of course as others have answered this can be done much more elegant using sum.
If you actually need the index for something else you could use enumerate like this:
def grades_sum(grades):
total = 0
for i, grade in enumerate(grades):
total += grade #or grades[i]
return total
Or if you don't care about retrieving the item at all
def grades_sum(grades):
total = 0
for i in range(len(grades)):
total += grades[i]
return total
You don't need to do grade[i] because you're already referencing the elements in the list - all you need to do it replace that with a plain old i
However, there is already a builtin function for this - sum
print(sum(grades))
for i in grades: iterates over the elements in grades. It doesn't iterate over the indexes.
To fix your code, just use i instead of grades[i].
Don't be afraid of using print statements. They're excellent for debugging code.

Categories

Resources