-1

I am writing a simple guessing number program and I want to use functions for different sections of the code. The first function decides on the range the user will guess between. I want to carry the value of user_input into the next function. The only way I know is to make user_input a global variable. Just wondering is there a better way of achieving this than making the variable global?

def decide_range():
    flag = True
    while flag:
        global user_input
        user_input = input("Enter a value for range ")
        if user_input.isdigit():
            user_input = int(user_input)
            if user_input < 2:
                print("Number must be 2 or greater to play this game")
                continue
            print(f"You have entered {user_input}")


            flag = False

        else:
            print("Not a valid integer")

def guess_Number():
    print("Guess a number between 1 and",user_input)
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
Colum
  • 1
  • 1
  • generally to the question of "should I use a global variable" the answer is usually no you should not ... (but you can)... try and use namespace encapsulations (probably write a class that has the members and functions you need) – Joran Beasley Aug 16 '21 at 20:30
  • A better way than using `global` instructions is creating an object instance that holds the value internally. This way the reference stays the same and you can modify the value without using `global`. In fact, I've used the equivalent of a `globals` class to hold all my shared values on occasion. – RufusVS Aug 16 '21 at 20:32
  • 1
    Does this answer your question? [Python Alternatives to Global Variables](https://stackoverflow.com/questions/11462314/python-alternatives-to-global-variables) – Woodford Aug 16 '21 at 20:34
  • I'd change "decide_range" to return the user's input, and then have "guess_number" call "decide_range". – Mark Lavin Aug 16 '21 at 20:35
  • You need to return to your materials on functions, and learn how to pass parameters and return values. – Prune Aug 16 '21 at 20:42

2 Answers2

1

You could have the first function return the value of the user_input, which could then be passed as a value into the second function. In this case, the second function just needs to know the user_input as a parameter.

def decide_range():
    flag = True
    user_input = 0
    while flag:
        user_input = input("Enter a value for range ")
        if user_input.isdigit():
            user_input = int(user_input)
            if user_input < 2:
                print("Number must be 2 or greater to play this game")
                continue
            print(f"You have entered {user_input}")
            flag = False
        else:
            print("Not a valid integer")
    return user_input

def guess_Number(user_input):
    print("Guess a number between 1 and", user_input)

number = decide_range()
guess_Number(number)
rohanphadte
  • 978
  • 6
  • 19
0

There is a bette way . . . create a class

class decide_range(object):
    def __init__(self):
        flag = True
        while flag:
            self.user_input = input("Enter a value for range ")
            if self.user_input.isdigit():
                self.user_input = int(self.user_input)
                if self.user_input < 2:
                    print("Number must be 2 or greater to play this game")
                    continue
                print(f"You have entered {self.user_input}")
                flag = False
            else:
                print("Not a valid integer")

    def guess_Number(self):
        print("Guess a number between 1 and", self.user_input)

decide_range().guess_Number()
It_is_Chris
  • 13,504
  • 2
  • 23
  • 41