3

I'm a Python newbie. I wrote this code, but heard that declaring global variables isn't a good practice. What would be in this case the right way to write this function?

index = 0

def level_selection():
    global index
    level = raw_input("Choose the desired level of difficulty: easy, medium or hard")
    if level.lower() == "easy":
        return level1
    if level.lower() == "medium":
        return level2
    if level.lower() == "hard":
        return level3 
    else:
        print "Error"
        index += 1
        if index < 3:
            return level_selection()
        return

level1 = "You selected easy"
level2 = "You selected medium"
level3 = "You selected hard"
aiven
  • 3,775
  • 3
  • 27
  • 52

3 Answers3

1

Another thing, if you really need global variables is to have a class member variable that holds your Index.

class VARS(object):
    index = 0  
VARS.index += 1
print(VARS.index)
>1
user1767754
  • 23,311
  • 18
  • 141
  • 164
1

if you're newbie to python i highly recommend you to use python version 3.
python reads the codes line by line, that means that you can not call a variable before assign it.
the global variables are accessible inside the functions and classes, or by other meaning the variables are inherited inside functions and classes and no need to use global.
as a good practice:

  • always try to use else statement if you are going to use if statement
  • pass the arguments to your functions for having more dynamic program instead of using static content.

so in your case the code is going to be:

index = 0 #global scope

def level_selection(dx):
    my_local_index = dx #creating a new variale

    level1 = "You selected easy" #local variables
    level2 = "You selected medium" # not accessible outside of this scope
    level3 = "You selected hard" 

    level = raw_input("Choose the desired level of difficulty: easy, medium or hard")
    if level.lower() == "easy":
        return level1
    if level.lower() == "medium":
        return level2
    if level.lower() == "hard":
        return level3 
    else:
        print "Error"
        my_local_index += 1
        if my_local_index < 3:
            return level_selection(my_local_index)
        else:
            exit()

# calling the function and passing a argument to it
level_selection(index) 
0

Your purpose on this program to allow user input up to 3 Error value.

You can pass index as argument that counts incorrect entry by user and retain this value by calling the function recursively

See the complete rewrite with that approach.

def level_selection(index):

    level1, level2, level3 = "level1", "level2","level3"

    level = input("Choose the desired level of difficulty: easy, medium or hard")
    if level.lower() == "easy":
        return level1
    if level.lower() == "medium":
        return level2
    if level.lower() == "hard":
        return level3 
    else:
        print("Error")
        index -= 1
        if index > 0:
            return level_selection(index)

    return

print(level_selection(3))
yash
  • 1,357
  • 2
  • 23
  • 34