Zacharias Traianos
Zacharias Traianos

Reputation: 1

Access a function variable from outside the function in python

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...

Upvotes: 0

Views: 134

Answers (4)

David Sorrell
David Sorrell

Reputation: 241

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#

Upvotes: 0

oppressionslayer
oppressionslayer

Reputation: 7214

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

Upvotes: 0

Evan Dilkes
Evan Dilkes

Reputation: 13

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

Upvotes: 0

Tal Avissar
Tal Avissar

Reputation: 10304

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

Upvotes: 1

Related Questions