help with printing a result from a function | Coursera Community
Coursera Header
Answered

help with printing a result from a function

  • 17 October 2020
  • 6 replies
  • 75 views

Badge +2

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)
V1 execution result

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
V2 execution result

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)
V3 execution result

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)
V4 execution result

 

View original

6 replies

Badge +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 ('your total:',total)
        
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.

 

hope this will helpful,

Badge +2

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

 

Badge +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 ('your total:',total)
        
print ('Start')
tot()
#print (count) #But I would like to print the results down here
print ('your total:',total)
print ('End')

 

 

output is also attached:

hope this will help

Userlevel 2
Badge +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)
V1 execution result

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
V2 execution result

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)
V3 execution result

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)
V4 execution result

 

Badge +2

Thank you very much for your help.

 

Badge +2

the global worked great. Thanks again.

Reply