Home > Mobile >  How do I use a variable as a parameter for python?
How do I use a variable as a parameter for python?

Time:04-18

I'm not an expert at coding in python3 and I got stuck trying to program a function that would receive user input and check that it was valid. The function uses the variable book_length or words_per_page as a parameter to later store the user input. The function is able to receive user input and properly check its value. There are no bugs or errors but the problem is that it does not keep the new numerical value inputted by the user, instead, it continues to use the original value of the variable from when it was originally declared and it's not supposed to do that. I've also tried declaring the variables before the definition of the function and inside the definition of the function but the result is the same. Pls, help.

Here is some of my code:

This image shows the code for my function as well as 2 different calls to the function

CodePudding user response:

Your function does not return any value. Variables declared inside the function are local variables hence modifications will also be within the scope of the defined function. You can do two things:

  1. Define a global variable and use that inside the function. (OR)
  2. Make your function return the desired value and store that value in the variable you want. It should look something like this:
def input_value_int_checker(<parameters>):
    <your code>
    
    return user_input

book_length = input_value_int_checker(<parameters>)
words_per_page = input_value_int_checker(<parameters>)
  • Related