0

I have a python function and would like to retrieve a value from outside the function. How can achieve that without to use global variable. I had an idea, if functions in python are objects then this could be a right solution?

def check_difficulty():
    if (difficulty == 1):
        check_difficulty.tries = 10
    elif (difficulty == 2):
        check_difficulty.tries = 5
    elif (difficulty == 3):
        check_difficulty.tries = 3


try:
    difficulty = int(input("Choose your difficulty: "))
    check_difficulty()

except ValueError:
    difficulty = int(input("Type a valid number: "))
    check_difficulty()

while check_difficulty.tries > 0:

I am new to python so excuse me...

4 Answers 4

1
def check_difficulty(difficulty):
    if (difficulty == 1):
        return 10
    elif (difficulty == 2):
        return 5
    elif (difficulty == 3):
        return 3

tries = 0
while tries > 0:
    difficulty = int(input("Choose your difficulty: "))

    tries = check_difficulty(difficulty)
    tries = tries - 1
Sign up to request clarification or add additional context in comments.

Comments

0

if you use a while loop and put everything inside in a structured way, a function will not be needed.

Comments

0

You can change this to a class to get your tries:

class MyClass:
  def __init__(self):
    self.tries = 0
  def check_difficulty(self, difficulty):
    if (difficulty == 1):
        self.tries = 10
    elif (difficulty == 2):
        self.tries = 5
    elif (difficulty == 3):
        self.tries = 3

ch = MyClass()

try:
    difficulty = int(input("Choose your difficulty: "))
    ch.check_difficulty(difficulty)

except ValueError:
    difficulty = int(input("Type a valid number: "))
    ch.check_difficulty(difficulty)


ch.tries
# 5

Comments

0

If you want the question answered within the construct of your current code simply put your try, except before the function. You can call a function anywhere in the code it doesn't ave to be after the function is created . So something like this:

try:
    difficulty = int(input("Choose your difficulty: "))
    check_difficulty()

except ValueError:
    difficulty = int(input("Type a valid number: "))
    check_difficulty()

def check_difficulty():
    if (difficulty == 1):
        check_difficulty.tries = 10
    elif (difficulty == 2):
        check_difficulty.tries = 5
    elif (difficulty == 3):
        check_difficulty.tries = 3


while check_difficulty.tries > 0:

however, I would have to agree with the other answers and just kind of put everything together within the same loop and you won't have to worry about this. I created a guessing game recently that actually had something similar to this. Here is the difficulty portion of that code:

def guessing_game():
    again = ''
# Define guesses/lives
    while True:
        try:
            guesses_left = int(input('How many guess would you like(up to 4)?: '))
            if 1 > guesses_left or guesses_left > 4:
                print('You must choose between 1 and 4 for your guess amount. Try again.')
                continue
            break
        except:
            print('You must enter a valid number between 1 and 4. Try again.')

# Define difficulty based on guesses_left
    difficulty = ''
    if guesses_left == 1:
        difficulty = 'Hard Mode'
    elif guesses_left == 2:
        difficulty = 'Medium Mode'
    elif guesses_left == 3:
        difficulty = 'Easy Mode'
    elif guesses_left == 4:
        difficulty = 'Super Easy Mode'

    print('You are playing on ' + difficulty + ' with ' + str(guesses_left) + ' guesses.')

#code continues under this line to finish#

2 Comments

Thank you for you answer, but i want to ask if the first approach is right or not. Is it ok to do something like that? 'check_difficulty.tries = ....' and then to call this outside of the function?
It all comes down to what the end goal is. You don't want to do something that works that will mess up something later and you just have to redo it. Think not just about the code you are typing right now but also about code that might also involve this function and write based on that. In the context of this question, yes. The first option is perfectly fine and in fact is a great way to understand how functions work and can be called anywhere in your code even within other funtions

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.