help with printing a result from a function | Coursera Community

# help with printing a result from a function

• 6 replies
• 78 views

+2
• Newcomer
• 3 replies

Hello everyone. I am having printing a result from a function from the code below:

def tot():
count = 0
sum = 0
while True:
num = input ('Please enter a number: ')
if num == 'done':
break
else:
try:
newnum = int(num)
except:
print ('Invalid Entry')
continue
count = count + 1
sum = sum + newnum
total = sum
print (count) This will print while the loop is going

print ('Start')
tot()
print (count) But I would like to print the results down here

print (total)
print ('End')

Thank you for any help.

icon

Best answer by Mateusz Kita 17 October 2020, 12:20

Hello.

First of all - you actually don’t need ‘total’ variable, as you already use ‘sum’ which is exactly the same in terms of final value.

If you just want to print both values once, from within the tot() function and at the end of its execution, you need to make sure the indentations are correct:

``def tot():    count = 0    sum = 0    while True:        num = input ('Please enter a number: ')        if num == 'done':            break        else:            try:                newnum = int(num)            except:                print ('Invalid Entry')                continue        count = count + 1        sum = sum + newnum    print(count)    print(sum)``

If you’d like to use these values outside of the function, you have basically two choices:

1. declaring variables as “global variables”
2. assigning values to another variable as a result of function execution (returned value)

In case of “global variables”:

``def tot():    global count    count = 0    global sum    sum = 0    while True:        num = input ('Please enter a number: ')        if num == 'done':            break        else:            try:                newnum = int(num)            except:                print ('Invalid Entry')                continue        count = count + 1        sum = sum + newnum``

In case of returned values (suggested type - tuple):

``def tot():    count = 0    sum = 0    while True:        num = input ('Please enter a number: ')        if num == 'done':            break        else:            try:                newnum = int(num)            except:                print ('Invalid Entry')                continue        count = count + 1        sum = sum + newnum    return (count, sum)``

Finally, you can combine all solutions described above into a single piece of code, and use them as you please:

``def tot():    global count    count = 0    global sum    sum = 0    while True:        num = input ('Please enter a number: ')        if num == 'done':            break        else:            try:                newnum = int(num)            except:                print ('Invalid Entry')                continue        count = count + 1        sum = sum + newnum    print(count)    print(sum)    return (count, sum)``

View original

### 6 replies

+2

the global worked great. Thanks again.

+2

Thank you very much for your help.

Userlevel 3
+6

Hello.

First of all - you actually don’t need ‘total’ variable, as you already use ‘sum’ which is exactly the same in terms of final value.

If you just want to print both values once, from within the tot() function and at the end of its execution, you need to make sure the indentations are correct:

``def tot():    count = 0    sum = 0    while True:        num = input ('Please enter a number: ')        if num == 'done':            break        else:            try:                newnum = int(num)            except:                print ('Invalid Entry')                continue        count = count + 1        sum = sum + newnum    print(count)    print(sum)``

If you’d like to use these values outside of the function, you have basically two choices:

1. declaring variables as “global variables”
2. assigning values to another variable as a result of function execution (returned value)

In case of “global variables”:

``def tot():    global count    count = 0    global sum    sum = 0    while True:        num = input ('Please enter a number: ')        if num == 'done':            break        else:            try:                newnum = int(num)            except:                print ('Invalid Entry')                continue        count = count + 1        sum = sum + newnum``

In case of returned values (suggested type - tuple):

``def tot():    count = 0    sum = 0    while True:        num = input ('Please enter a number: ')        if num == 'done':            break        else:            try:                newnum = int(num)            except:                print ('Invalid Entry')                continue        count = count + 1        sum = sum + newnum    return (count, sum)``

Finally, you can combine all solutions described above into a single piece of code, and use them as you please:

``def tot():    global count    count = 0    global sum    sum = 0    while True:        num = input ('Please enter a number: ')        if num == 'done':            break        else:            try:                newnum = int(num)            except:                print ('Invalid Entry')                continue        count = count + 1        sum = sum + newnum    print(count)    print(sum)    return (count, sum)``

+1

def tot():
count = 0
sum = 0
while True:
num = input ('Please enter a number: ')
if num == str('') :
print('program ended')
break
else:
try:
newnum = int(num)
except:
print ('Invalid Entry')
continue
count = count + 1
sum = sum + newnum
global total
total = sum
print ('your number of count:',count) #This will print while the loop is going

print ('Start')
tot()
#print (count) #But I would like to print the results down here
print ('End')

output is also attached:

hope this will help

+2

how would you be able to print it after the loop has ended? or once you end the loop?

+1

since provided explanation of  what you want was not enough but hope this will help you!

def tot():
count = 0
sum = 0
while True:
num = input ('Please enter a number: ')
if num == str('') :
print('program ended')
break
else:
try:
newnum = int(num)
except:
print ('Invalid Entry')
continue
count = count + 1
sum = sum + newnum
total = sum
print ('your number of count:',count) #This will print while the loop is going

print ('Start')
tot()
#print (count) #But I would like to print the results down here
print ('End')

output is also attached:

and if you wanted to print total outside the function tot then you can do this by declaring total variable as global.