Issue
I am wondering how I would go about getting different variable names into a function if that makes sense. I am trying to create a "portable" loop to make things easier and more structured in a text based adventure I am creating. Any help is appreciated. Code I have tried is shown below for an idea of what im trying to achieve.
def incorrectAnswerLoop(answers, variable_name):
while True:
user_input=input()
if answers in user_input:
variable_name = user_input
break
else:
print("incorrect input")
continue
incorrectAnswerLoop({"katana", "claymore", "dagger"}, sword.type)
Solution
Use return
instead of trying to modify one of the arguments. (You can use mutable args, or even mutate mutable objects in the outer scope, but don't -- just return the value you want to return.)
If your function returns the value, then the caller can simply assign the returned value to whatever variable it wants.
def incorrectAnswerLoop(answers):
while True:
user_input = input()
if user_input in answers:
return user_input
else:
print("incorrect input")
sword.type = incorrectAnswerLoop({"katana", "claymore", "dagger"})
Answered By - Samwise
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.