running python script again based on user answer using decorator
Question:
Im trying to run the script again, based on user input(y/n). In my main file Im calling couple of functions from another file, with the decorator Ive seen its usage in older post here, but cant find why Im getting an error: "
game()
TypeError: ‘NoneType’ object is not callable "
def restartable(func):
def wrapper(*args, **kwargs):
answer = 'y'
while answer == 'y':
func(*args, **kwargs)
while True:
answer = input("Play Again? y/n")
if answer in ('y', 'n'):
break
else:
print("invalid answer")
return wrapper()
@restartable
def game():
items = ["X", "Y", "Z"]
balance = support.get_deposit()
deposit, balance = support.accept_bet(balance)
spin_machine = support.spin_machine(items)
support.matrix_out(spin_machine)
coef = support.check_col(spin_machine)
support.calculate_money(balance, deposit, coef)
game()
I know this can be done in a While loop and many different ways, I just wanted it done with decorator :/
Answers:
It’s because your return wrapper
line is miss indented, and you should return the function without calling it.
def restartable(func):
def wrapper(*args, **kwargs):
answer = 'y'
while answer == 'y':
func(*args, **kwargs)
while True:
answer = input("Play Again? y/n")
if answer in ('y', 'n'):
break
else:
print("invalid answer")
return wrapper
should work.
Im trying to run the script again, based on user input(y/n). In my main file Im calling couple of functions from another file, with the decorator Ive seen its usage in older post here, but cant find why Im getting an error: "
game()
TypeError: ‘NoneType’ object is not callable "
def restartable(func):
def wrapper(*args, **kwargs):
answer = 'y'
while answer == 'y':
func(*args, **kwargs)
while True:
answer = input("Play Again? y/n")
if answer in ('y', 'n'):
break
else:
print("invalid answer")
return wrapper()
@restartable
def game():
items = ["X", "Y", "Z"]
balance = support.get_deposit()
deposit, balance = support.accept_bet(balance)
spin_machine = support.spin_machine(items)
support.matrix_out(spin_machine)
coef = support.check_col(spin_machine)
support.calculate_money(balance, deposit, coef)
game()
I know this can be done in a While loop and many different ways, I just wanted it done with decorator :/
It’s because your return wrapper
line is miss indented, and you should return the function without calling it.
def restartable(func):
def wrapper(*args, **kwargs):
answer = 'y'
while answer == 'y':
func(*args, **kwargs)
while True:
answer = input("Play Again? y/n")
if answer in ('y', 'n'):
break
else:
print("invalid answer")
return wrapper
should work.