0

This is a one script program I made in repl.it I will show where most of my errors are. When I try to do this:

words = ["worda", "wordb", "wordc", "wordd"]
def show_help():
  print("Type one of the four words that person says into the input.")
  print("Type the word you want to tally into the console. Note that the words are case sensitive")
  print("Type HELP if you need this to show")



def main(inpt):
    a = 0
    b = 0
    c = 0
    d = 0
    while True:
        if inpt == 'worda':
            a += 1
            print("{} has spurt. It now has: {} spurts".format(inpt, a))
            break
            main(input("> "))
        if inpt == 'wordb':
             a += 1
             print("{} has spurt. It now has: {} spurts".format(inpt, b))
             break
             main(input("> "))
 show_help()
 main(input("> "))

What happens here it just says "Worda has spurt. It now has: 1 spurt" And it only said that. It doesn't add the times I needed it to count.

words = ["worda", "wordb", "wordc", "wordd"]
a = 1
b = 1
c = 1
d = 1
def show_help():
    print("Type one of the four words that person says into the input.")
    print("Type the word you want to tally into the console. Note that the words are case sensitive")
    print("Type HELP if you need this to show.")

def main(inpt): #The main block that is causing errors.
    while True:
        if inpt == 'worda': 
           nonlocal a #what I think will make the program reach out to the variables
           a += 1
           print("{} has spurt. It now has: {} spurts".format(inpt, a))
           break
        if inpt == 'wordb':
          nonlocal b #I get a syntax error here saying "no binding for nonlocal 'b' found"
          b += 1
          print("{} has spurt. It now has: {} spurts".format(inpt, b))
          break
          main(input("> "))
show_help()
main(input("> "))

How do I fix this and make it add like: a += 1, then in the background it is a = 2, then 3, then 4, ect?

1 Answer 1

1

You need the global keyword to access them:

words = ["worda", "wordb", "wordc", "wordd"]

a = 1
b = 1
c = 1
d = 1


def show_help():
    print("Type one of the four words that person says into the input.")
    print("Type the word you want to tally into the console. Note that the words are case sensitive")
    print("Type HELP if you need this to show.")


def main(inpt):  # The main block that is causing errors.
    while True:
        if inpt == 'worda':
            global a
            a += 1
            print("{} has spurt. It now has: {} spurts".format(inpt, a))
            break
        if inpt == 'wordb':
            global b
            b += 1
            print("{} has spurt. It now has: {} spurts".format(inpt, b))
            break
            main(input("> "))
show_help()
main(input("> "))

Demo.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.