-2

I need to generate a random number via a function, return it to another function and then run that second function multiple times with the same random number I got in the first place. But the random number generator makes a new one whenever my function summons it for the random number. (Python3.7)

import random
from sys import exit

def rand():
    n = random.randint(1, 1001)
    return n

n = rand()

def isdigit(s):
    try:
        float(s)
        return True
    except ValueError:
        return False

def guess():
    g = input("> ")

    if isdigit(g):
        return int(g)

    elif not isdigit(g):
        print("\tPlease enter a number.")
        guess()


def islower():
    print("\tThe number is GREATER than that!")
    process()

def ishigher():
    print("\tThe number is SMALLER than that!")
    process()

def gotit():
    print(f"\tCongratulations! You got it! The number was {n}.")

    print("\tPlay another hand? y/n")
    again = input("> ")
    if again == "y":
        newgame()
    elif again == "n":
        print("\tHave fun!")
        exit(0)


def process():

    g = guess()

    if g > n:
        ishigher()

    elif g < n:
        islower()

    elif g == n:
        gotit()

def newgame():
    print("\tI have chosen my number. Start!")
    process()


newgame()
Tripasect
  • 101
  • 2
  • 3
    Post some code? Sounds like you just made a mistake somewhere. – Dan Oct 04 '19 at 09:09
  • Sounds like you need to remove the generation of random number just above the loop where you run your second function multiple times. – GeoMSI Oct 04 '19 at 09:12
  • How do you know that `n` is changing every-time you call `process`? Have you tried putting `print(n)` inside? Also, this is not a clear way to write code. I would suggest using [`if __name__ == __main__":`](https://stackoverflow.com/q/419163/1011724). You should set the value for `n` inside `newgame()`, not just in the wild like you have. And then pass the value around i.e. `process()` should take `n` as an argument. If you don't want to pass it around then make an object and set `n` as a state variable or if you want to stick with your functional pattern, create `process` as a closure. – Dan Oct 04 '19 at 10:20
  • I think `n` should change every time the game restarted, but it does not change at all. When you enter `y` to play again, the random number is still the same one before which makes no sense. To change it, user must close the game and re run the program. – GeoMSI Oct 04 '19 at 10:48

2 Answers2

2

It's hard to give you an answer without a simple example of code, but the first thing I think of is : can't you store the result of the random generator in a variable and use this variable as an input of your second function ? Something like :

def get_a_random_number():
   # Whatever method you use to generate your random number
   return random_number

def your_other_function(a_random_number):
  # Do what you need with the random number

my_random_number = get_a_random_number()

# Repeatedly use your other function
your_other_function(my_random_number)
your_other_function(my_random_number)
your_other_function(my_random_number)
your_other_function(my_random_number)
...
Rowin
  • 435
  • 2
  • 9
  • Well, technically what you have written would make no difference than calling a random generator multiple times. Whenever your_other_function calls my_random_number it goes to get_a_random_number then that calls for another random number with the help of random module. You just made a train. – Tripasect Oct 04 '19 at 09:55
  • 1
    ```my_random_number``` is a variable, not a callable, and it is not evaluated each time it is used by ```your_other_function()``` – Rowin Oct 04 '19 at 10:06
  • You're right. I was in mistake. – Tripasect Oct 04 '19 at 10:14
  • But how can I update this random number (assign a new one to that variable) after I have done a series of code? – Tripasect Oct 04 '19 at 10:16
  • 1
    Just call `my_random_number = get_a_random_number()` again – Dan Oct 04 '19 at 10:21
0

If you initialized the two functions just once

rand_number = random_generator()
other_func(rand_number)

Python script will return a new value each time when it ran. So What you can do is call the other_func multiple time and pass the variable or loop the seond function multiple times.

for _ in range(x):
    other_func(rand_number)
Govinda Malavipathirana
  • 1,095
  • 2
  • 11
  • 29