Home > Enterprise >  When using functions and ' while True' in python , i get a 'None' in result , wh
When using functions and ' while True' in python , i get a 'None' in result , wh

Time:12-24

I'm learning python , and decided to write a program which takes a undefined number of inputs and gives the 'sum' and 'max' from math module , where the inputs are first stored in a list (b) and then passed to a function . I'm not sure if there is an another way to do this ( actually used *args and a lot of 'if' conditions before i could get a desired result, and my current code is the closest to it) I'm fairly new to stackoverflow too , so tips on how i presented my question and how i can improve it will help too~

def dc(args):
        print('sum :', sum(args) ,'Max :', max(args))
        return
b=[]
while True:
        a = input('->')
        if a == "":
                break
        b.append(int(a))

 
 
print(dc(b))

#so function dc returns sum and max. And used while True keep giving input untill a blank line - "" is given before adding them to list b[]

what I expected ...

->1
->2
->3
->
sum : 6 Max : 3

What I got ...

->1
->2
->3
->
sum : 6 Max : 3
None

And i don't understand where the None came from

CodePudding user response:

Try out this code

def dc(args):
        return 'sum :', sum(args) ,'Max :', max(args)
b=[]
while True:
        a = input('->')
        if a == "":
                break
        b.append(int(a))

 
 
print(dc(b))

This happened because you have returned a None object and things actually go fine with the print() function. Here

  • Related