Alfred H.
Alfred H.

Reputation: 103

how make 3 functions to run in one program

For answers please make it as simple as possible refrain from using any technique that is too advanced as I am still learning Python and do not want to jump too far ahead...

For the def main program, I am trying to run all 3 functions in this "Main", the Summation and Multiply functions is self explanatory, the CreateIntList is suppose function takes in a sentinel value (an integer), and it takes in a sentinal value(int) and return a list of integers. it prompts the user to create a list of integers, using the provided sentinel value as the number the user enters to exit the creation of the list.

For each list:
o Tell the user which number list they are creating (starting at 1)
o Ask the user for their desired sentinel value
o Create a lists of integers (using the given sentinel value)
o Calculate the summation
o Calculate the product
o Print out:
 The entire list
 The summation of the list
 The product of the list



def summation(intlist):
    output = 0
    for x in range(len(intlist)):
        output += intlist[x]
    return output

def multiply(intlist):
    if len(intlist) == 0:
        output = 0
        return output
    else:
        output = 1
        for x in range(len(intlist)):
            output = intlist[x] * output
        return output

def createIntList(myInt):
    createlist = []
    myInt = int(input("What do you want the sentinel to be?"))
    addNum = None
 while myInt != addNum:
        addNum = input("Please enter a number, ", myInt, "to stop: ")
        createlist.append(addNum)
    return createlist

def main():
    print("Welcome to the Simple Math Helper")
    listIteration = int(input("How many list would you like to create? "))
    x = 0
    for x in range(len(listIteration)):
        print("Your are creating list #", x)
        createIntList(myInt)
        multiply(createIntList)
        summation(createIntList)
        x = x + 1
    print("Thank you for using the Simple Math Helper")


main()

Projected output:

Welcome to the Simple Math Helper
How many lists would you like to create? 1
You are creating list #1
What do you want the sentinel to be? 0
Please enter a number, 0 to stop: 1
Please enter a number, 0 to stop: 2
Please enter a number, 0 to stop: 3
Please enter a number, 0 to stop: 4
Please enter a number, 0 to stop: 5
Please enter a number, 0 to stop: 6
Please enter a number, 0 to stop: 7
Please enter a number, 0 to stop: 8
Please enter a number, 0 to stop: 9
Please enter a number, 0 to stop: 10
Please enter a number, 0 to stop: -9
Please enter a number, 0 to stop: -3
Please enter a number, 0 to stop: -1
Please enter a number, 0 to stop: 0
For the list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -9, -3, -1]
The summation is 42
The product is -97977600
Thank you for using the Simple Math Helper

Upvotes: 1

Views: 84

Answers (1)

Arthur Zopellaro
Arthur Zopellaro

Reputation: 158

Samira N already gave you some feedback, but there are many other problems with your code. I commented all lines with errors and corrected them for you.

def summation(intlist):
    output = 0  #you are correct, as you are using it below, you need to initialize beforehand
    #for x in range(len(intlist)): # see below for a better way to use list in a 'for'
    for x in intlist: # 'x' now represents the proper element inside the list, iterating each element
        #output += intlist[x] #see below on how to use 'x' with the new 'for'
        output += x
    return output

def multiply(intlist):
    if len(intlist) == 0:
        #output = 0 # no need for 'output'
        #return output # same
        return 0
    else:
        output = 1# same here, you are using output below, so you need to initialize beforehand. Nice!
        #for x in range(len(intlist)): # same as before, see below for a better way to use 'for' with lists
        for x in intlist:
            #output = intlist[x] * output # same as before, use 'x' instead of 'intlist[x]' with the new 'for'
            output = x * output
        return output

#def createIntList(myInt): #you are initializing myInt in the input, so you dont need it here
def createIntList():
    #createlist = [] # you should use better names for your variables
    new_list = []
    #myInt = int(input("What do you want the sentinel to be?")) # pick a better name for myInt
    sentinel = int(input("What do you want the sentinel to be?"))
    #addNum = None # same, better name
    number = None
    #while myInt != addNum: #see below for the 'while' with the new variable names
    while sentinel != number:
        #number = input("Please enter a number, ", sentinel, "to stop: ") # you need to use + to append, but you need to parse it to str as 'sentinel' is an int
        number = int(input("Please enter a number, " + str(sentinel) + "to stop: ")) # same you did with 'sentinel', need to add 'int(' so you make sure it returns a number
        #createlist.append(addNum) #same code below, but with the new variable names
        if sentinel != number: # so you don't add the sentinel to the list
            new_list.append(number)
    return new_list

def main():
    print("Welcome to the Simple Math Helper")
    listIteration = int(input("How many list would you like to create? "))
    #x = 0 # x is already being initialized/declared in the 'for' below
    #for x in range(len(listIteration)): # listIteration is already an int, so you don't need to use len()
    for x in range(listIteration):
        print("Your are creating list #", x+1) #x+1 because x starts as 0, +1 ensures it will show 'Your are creating list #1' and so on
        #createIntList(myInt) # you are calling the function with a variable that doesnt exist: myInt
        int_list = createIntList() # as createIntList returns something, lets use int_list to save the returned list
        #multiply(createIntList) # you need to send as paramter a variable (not the function itself)
        multiplication_output = multiply(int_list) # you are returning an int, so you need to store it somewhere
        #summation(createIntList) # send a variable, not the function
        sum_output = summation(int_list) # same as before store the return somewhere
        # x = x + 1 # no need for this, the 'for' does it already
        print("For the list ", int_list)
        print("The summation is ", sum_output)
        print("The product is ", multiplication_output)
    print("Thank you for using the Simple Math Helper")


main()

I tried to keep things simple as you asked:

For answers please make it as simple as possible refrain from using any technique that is too advanced as I am still learning Python and do not want to jump too far ahead...

This is how I would do, though:

def summation(int_list):
    return sum(int_list) #sum() is a pretty good built-in function :)

def multiply(int_list):
    if int_list: #better way to write 'if len(intlist) != 0:'
        output = 1
        for x in int_list:
            output = x * output
        return output
    else:
        return 0

def create_list(list_number):
    print('Your are creating list #{}'.format(list_number))
    new_list = []
    sentinel = int(input('What do you want the sentinel to be? '))
    number = None
    while number != sentinel:
        #str.format() is a function that I love.
        #Check it out: https://pyformat.info/
        number = int(
        input('Please enter a number ({} to stop): '.format(sentinel)))
        if number != sentinel:
            new_list.append(number)
    return new_list

def main():
    print('Welcome to the Simple Math Helper')
    listIteration = int(
    input('How many list would you like to create? '))

    for x in range(listIteration):
        int_list = create_list(x+1)
        multiplication_output = multiply(int_list)
        sum_output = summation(int_list)
        print('''For the list {}
The summation is {}
The product is {}'''.format(int_list, sum_output,multiplication_output))

    print('Thank you for using the Simple Math Helper')

main()

Upvotes: 1

Related Questions