#Help with Functions in Python

20 messages · Page 1 of 1 (latest)

restive geyserBOT
#

@low prairie

Jeditwo Uploaded Some Code

I have been working on a python project today and I do not understand what is going wrong with my program.
My goal is to square the list three times and print it after every repetition, but after it goes through my function it returns as None instead of a list. I am trying to modify the original list, and not use the return statement.

Attachment: Squaring\_List.py
import time

def square(values):
#Creates a function called 'square' that takes a parameter called 'values'.
    for i in range(len(values)):
        values[i] = int(values[i] * values[i])
        #takes a number from the list 'values', squares it, saves it in its old position, and moves onto the next one.    
def main():
#Creates a function called 'main'.    
    n = int(input("How many numbers would you like to square? "))
    values = list(range(1,n+1))
    print("The original list is: ", values, ".")
    for i in range(3):
        print("The squared list is: ", square(values), ".")

main()

Attachment: Squaring\_List-output.txt
Python 3.11.1 (tags/v3.11.1:a7a450f, Dec  6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
==== RESTART: D:\School\UMA\CIS 110\Python Projects\Week 5\Squaring_List.py ====
How many numbers would you like to square? 8
The original list is:  [1, 2, 3, 4, 5, 6, 7, 8] .
The squared list is:  None .
The squared list is:  None .
The squared list is:  None .

low prairie
#
import time

def square(values):
#Creates a function called 'square' that takes a parameter called 'values'.
    for i in range(len(values)):
        values[i] = int(values[i] * values[i])
        #takes a number from the list 'values', squares it, saves it in its old position, and moves onto the next one.    
def main():
#Creates a function called 'main'.    
    n = int(input("How many numbers would you like to square? "))
    values = list(range(1,n+1))
    print("The original list is: ", values, ".")
    for i in range(3):
        print("The squared list is: ", square(values), ".")

main()

#

The issue I am having is with the first function square(values), it seems to make my list 'values' a None data type instead of a list after it goes through it, and I don't know why.

river glen
#

If you directly print a function call, python will print the call's return value. I'm your case, your function doesn't return anything so it uses the default None as it's return value which gets printed out

low prairie
#

So how do I get it to directly alter the list that is passed into it? I was specifically told that I don't need to use return.

river glen
#
import time

def square(values):
#Creates a function called 'square' that takes a parameter called 'values'.
    for i in range(len(values)):
        values[i] = int(values[i] * values[i])
        #takes a number from the list 'values', squares it, saves it in its old position, and moves onto the next one.    
def main():
#Creates a function called 'main'.    
    n = int(input("How many numbers would you like to square? "))
    values = list(range(1,n+1))
    print("The original list is: ", values, ".")
    for i in range(3):
        square(values)
        print("The squared list is: ", values, ".")

main()

#

Try this

#

See if you understand how it works

low prairie
#

I think I get it

#

You have to do the function call on a separate line, otherwise it returns as None.

#

Does it not overwrite the original list until the function is finished it's call?

river glen
#

What happens is

#

When you have print(....., square(values), ...)

#

Python will reach that line of code and see that within the print function call, you are calling the square function

#

So it runs that square function (changing the values in the list) with the square function returning None at the end

#

So now your list has changed

#

But the square(values) returns None, so the print becomes something like print(..., None, ...)

#

Even if your list had been changed

low prairie
#

Ah okayt