Python Forum
How to test a function that is supposed to return NoneType?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to test a function that is supposed to return NoneType?
#1
Hey all. I'm working on an assignment from an intro to Python course on Coursera and I'm trying to check all my functions. I have a function, shown below, that is supposed to return type NoneType. I am trying to test the function in the shell by creating a variable used in the function (player_info) and populating that variable with required information, and then seeing if my function changes the values stored in the variable. So far, I've had no luck. I referenced/looked up what a few other people have done for this function and I think that my code is good but I would ultimately like to know how I can test this function to be sure.

def update_score(player_info, word):
    """ ([str, int] list, str) -> NoneType

    player_info is a list with the player's name and score. Update player_info
    by adding the point value word earns to the player's score.

    >>> update_score(['Jonathan', 4], 'ANT')
    
    >>> update_score(['Anthony', 4], 'JOBS')

    """
    ## How to test?  In shell I tried to assign str and int values to
    ## player_info to see if this function would change the value stored at
    ## player_info[1] but had no luck

    player_info = player_info[1] + word_score(word)
The function calls another function (word_score) and that function works correctly as far as I can tell (have messed with it in the shell a couple different ways). The other variable, player_info, is in another file that is supposed to be the driver for this word-search game, and is a file that I was provided with in the MOOC course materials.
Reply
#2
As such, the function is not correct. Line 16 should be
player_info[1] = player_info[1] + word_score(word)
Giving a new value to the local variable by a statement such as player_info = ... cannot change anything out of the function.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  nested function return MHGhonaim 2 601 Oct-02-2023, 09:21 AM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,275 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  function return boolean based on GPIO pin reading caslor 2 1,166 Feb-04-2023, 12:30 PM
Last Post: caslor
  how do I return Max Test result + corresponding student name from an excel dataset? sean1 3 1,250 Jan-16-2022, 09:07 PM
Last Post: snippsat
  return vs. print in nested function example Mark17 4 1,730 Jan-04-2022, 06:02 PM
Last Post: jefsummers
  Python class doesn't invoke setter during __init__, not sure if's not supposed to? mtldvl 2 3,313 Dec-30-2021, 04:01 PM
Last Post: mtldvl
  How to invoke a function with return statement in list comprehension? maiya 4 2,816 Jul-17-2021, 04:30 PM
Last Post: maiya
  How to test and import a model form computer to test accuracy using Sklearn library Anldra12 6 3,109 Jul-03-2021, 10:07 AM
Last Post: Anldra12
  Function - Return multiple values tester_V 10 4,432 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Get return value from a threaded function Reverend_Jim 3 16,989 Mar-12-2021, 03:44 AM
Last Post: Reverend_Jim

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020