-2

I need to know how to pass the invalid_input_count to the run_todo function it just errors and says Unresolved reference 'invalid_input_count'

import sys

todo_list = [
    "go shopping",
    "do homework",
    "do excercise",
]

invalid_input_count = 0

def run_todo():
    wanna_run = input("Would you like to run the to do list programme?\nYes or No? ")
    if wanna_run.lower() == "yes":
        print(*todo_list, sep = ", ")
        add_or_remove = input("Would you like to add or remove an instruction?\nadd or remove?")

    elif wanna_run.lower() == "no":
        print("See you later!")
        sys.exit()

    else:
        invalid_input_count += 1
        while invalid_input_count <= 3:
            print("Invalid input! You have " + str(3-int(invalid_input_count)) + " attempts left.")
            run_todo()
        else:
            print("Too many invalid inputs!\nShutting down...")
            sys.exit()

run_todo()
BThom238
  • 1
  • 1
  • Have a quick internet search for ‘pass variable into function + Python’. Note: Beware and (generally) avoid the posts recommending the use of a global variable. – S3DEV Nov 09 '20 at 20:03
  • 1
    Does this answer your question? [Can not increment global variable from function in python](https://stackoverflow.com/questions/10506973/can-not-increment-global-variable-from-function-in-python) – mkrieger1 Nov 09 '20 at 20:04

3 Answers3

0

You need to pass the variable in as an argument to the function.

def run_todo(invalid_input_count):

and if you want to alter the variable for use outside of the function you can just return the variable

or you can define the variable inside the function if it is only going to be used in the run_todo() function

def run_todo():
    invalid_input_count = 0

When you call the function, make sure to include the variable you're passing in:

run_todo(invalid_input_count)
BWallDev
  • 345
  • 3
  • 13
  • I tried def run_todo(invalid_input_count): but it doesnt work only error TypeError: run_todo() missing 1 required positional argument: 'invalid_input_count' – BThom238 Nov 09 '20 at 20:26
  • When you call the function, make sure to include the argument: run_todo(invalid_input_count) – BWallDev Nov 09 '20 at 20:28
0

When modifying a global variable inside a function, you have to specify it's global.

def run_todo():
    global invalid_input_count

Or, considering how your function look like, just declare the variable directly in the function

def run_todo():
    invalid_input_count = 0

This won't change the global variable though.

Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
  • No no no ... please no. Why encourage the use of a global variable? Oh please no. – S3DEV Nov 09 '20 at 20:04
  • 1
    @S3DEV There's no encouragement; it's a solution to the problem. I've also made sure to specify the absolute optimal solution right underneath it. – Ted Klein Bergman Nov 09 '20 at 20:06
  • @Ted Klein Bergman only problem is if i set invalid_input_count = 0 in run_todo function it will keep resetting itself to 0 instead of adding 1 – BThom238 Nov 09 '20 at 20:23
  • @BThom238 Yes, it'll work as your question is currently written, but if you have other use cases it won't. – Ted Klein Bergman Nov 09 '20 at 20:27
0

I would recommend you to do something like this to reduce confusion.

import sys

todo_list = [
    "go shopping",
    "do homework",
    "do excercise",
]

invalid_input_count = 0

def run_todo():
    wanna_run = input("Would you like to run the to do list programme?\nYes or No? ")
    if wanna_run.lower() == "yes":
        print(*todo_list, sep = ", ")
        add_or_remove = input("Would you like to add or remove an instruction?\nadd or remove?")
        return False
    elif wanna_run.lower() == "no":
        print("See you later!")
        sys.exit()
    else: return True

while run_todo():
    invalid_input_count += 1
    if invalid_input_count <= 3:
        print("Invalid input! You have " + str(3-int(invalid_input_count)) + " attempts left.")
    else:
        print("Too many invalid inputs!\nShutting down...")
        sys.exit()

The output of this will be as follows:

Would you like to run the to do list programme?
Yes or No? banana
Invalid input! You have 2 attempts left.
Would you like to run the to do list programme?
Yes or No? carrot
Invalid input! You have 1 attempts left.
Would you like to run the to do list programme?
Yes or No? friend
Invalid input! You have 0 attempts left.
Would you like to run the to do list programme?
Yes or No? cat
Too many invalid inputs!
Shutting down...

Or when you choose Yes, you can get this:

Would you like to run the to do list programme?
Yes or No? Yes
go shopping, do homework, do excercise
Would you like to add or remove an instruction?
add or remove?add

Or when you choose No, you can get this:

Would you like to run the to do list programme?
Yes or No? no
See you later!

What I am trying to do here is to call the run_todo() function with a return statement as True or False. The while statement will call the function. The counter is inside the while statement and thus does not have to deal with passing the variables across.

The recommendation from @BWallDev is minimal code change and will also work.

Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
  • Thanks that explanation was perfect! Just the kind of answer i was looking for : ) – BThom238 Nov 10 '20 at 20:40
  • Excellent. If the answer provided you what you wanted, please dont forget to upvote and mark the [answer](https://stackoverflow.com/help/someone-answers) as resolved – Joe Ferndz Nov 10 '20 at 21:42