3

(I'm a beginner in Python, just so you know)

What I'm trying to do: I want to keep count of how many times a user choose a wrong option and if it exceeds a number of times, he fails.

My approach was to store the count in a variable within a function and check with if/else statement if the number of times is exceeded.

Part of the code:

    choice = int(input("> "))

if choice == 1:
    print("This is the wrong hall.")
    increment()
elif choice == 2:
    print("This is the wrong hall.")
    increment()
elif choice == 3:
    hall_passage()
else:
    end("You failed")

COUNT = 0
def increment():
    global COUNT
    COUNT += 1

increment()

print(COUNT)

The increment part is from this thread and read using global scope is not a good practice.

The part I don't really understand is how you store count in a variable and it remembers the last value every time the function runs.

What is the best way to do this?

user1607016
  • 453
  • 2
  • 5
  • 20
  • 1
    Why does `COUNT` need to be in a function? – DavidG Dec 07 '17 at 15:04
  • 2
    if you want to keep state, use a `Class` – Maarten Fabré Dec 07 '17 at 15:05
  • the question you're asking is best answered by writing your own `class` which a more advanced topic. Although using global variables is a bad thing to do, it would probably be the way to start out doing what you want – Ryan Haining Dec 07 '17 at 15:08
  • 1
    Worth adding here that "using global variables is bad" shouldn't be taken as a hard-fast rule. There are situations that it makes easier, situations that it complicates. There are definitely uses for global variables, otherwise they wouldn't have been allowed in python in the first place. –  Dec 07 '17 at 15:10
  • 1
    You don't need to put count in a function. Just set count to 0 at the start and do `count += 1` instead of `increment()`. Your way doesn't work at the moment because `increment` is defined after it is used – Farhan.K Dec 07 '17 at 15:18
  • Not related to the question you are asking, but you may want to look through - [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – wwii Dec 07 '17 at 18:57

3 Answers3

2

maybe something like this...

class Counter():
    def __init__(self):
        self.counter = 0

    def increment(self):
        self.counter += 1

    def reset(self):
        self.counter = 0

    def get_value(self):
        return self.counter


mc = Counter()

while mc.get_value() < 3:
    v = int(input('a number: '))
    if v == 1:
        print('You won!')
        mc.counter = 3
    else:
        print('Wrong guess, guess again...')
        if mc.counter == 2:
            print('Last guess...')
        mc.increment()  
ahed87
  • 1,240
  • 10
  • 10
2

Adapting this answer you could utilize the functions own __dict__. Note: if you didn't come across the @ syntax yet search for "python", "decorator":

import functools as ft

def show_me(f):
    @ft.wraps(f)
    def wrapper(*args, **kwds):
        return f(f, *args, **kwds)
    return wrapper

@show_me
def f(me, x):
    if x < 0:
        try:
            me.count += 1
        except AttributeError:
            me.count = 1
        print(me.count, 'failures')

f(0)
f(-1)
f(1)
f(-2)

Output:

1 failures
2 failures
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
  • 2
    This is probably pretty helpful for some users, but OP stated they're a beginner in Python. I think wrappers may be just a bit too hard to understand. – Daniel Dec 07 '17 at 15:39
  • 1
    @Coal_ I take your point. Still, there's no harm in showing a beginner the proper way and let them decide for themselves whether it is too tough or not. And as you say, it may be helpful to others. – Paul Panzer Dec 07 '17 at 15:49
0

What do you think about this solution?

If you put here while loop you will forced on user to put correct answer.

Also i placed between if/elif/else statements input function.

count - That variable is counting wrong options

choice = int(input("> "))
count =0
while choice !=3:
    if choice == 1:
        print("This is the wrong hall.")
        count += 1  
        choice = int(input("> ")) # Also i place in the if/elif/else statements input function
    elif choice == 2:
        print("This is the wrong hall.")
        count +=1
        choice = int(input("> "))
    elif choice == 3:
        hall_passage()
    else:
        end("You failed")
        count += 1
        choice = int(input("> "))

print(count)
Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117
Spox
  • 11
  • 2