1
#timing game

import random
import time
import numpy as np

def player_interval_input():
    error1 = True
    # main while loop, checks if input is in range
    error2 = True
    # while loop is inside of error1, checks if input is an int or float
    while error1:
        interval = input("enter interval between 0 and 3:")
        while error2:
            try:
                interval = float(interval)
                error2 = False
            except ValueError:
                print("invalid input")
        if interval in np.arange(0,3,0.25):
            error1 = False
        else:
            print("interval has to be in between 0 and 3, try again")

def min_max_input():
    error3 = True
    # input for min and max range of random numbers
    while error3:
        range_min = input("enter minimum:")
        range_max = input("enter maximum:")
        if range_min.isdigit() or range_max.isdigit():
            range_min = int(range_min)
            range_maxb = int(range_max)
            error3 = False
        else:
            print("invalid input")

start = input("press 1 to start:")
try:
    start =int(start)
except ValueError:
    print("invalid input, try again")
if start == 1:
    player_interval_input()
    min_max_input()

answer = random.randrange(range_min,range_max)
print("press G when the number shown is " + str(answer))
print()

#main function, runs the guessing program
run = True
while run:
    time.sleep(interval)
    randnum = random.randint(range_min,range_max)
    print(randnum)

I'm trying to make a small little game where the objective is to press a key when a certain number shows up. I defined 2 functions, function player_interval_input() to let the player decide the interval/speed at which the numbers show up, and function min_max_input() to let the player decide the range of numbers that show up.

I made three values - interval, range_min and range_max which are all supposed to be integers or floats that the user inputs in these functions, but unfortunately they don`t show up as defined values in the later part of the code.

Why does this happen, and how to I fix it?

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
llama42
  • 11
  • 1

2 Answers2

1

to fix the error you should just need to define the variables at the start and along as you run min_max_input() and player_interval_input() before you use any for the three variables then it will be fine

#timing game

import random
import time
import numpy as np

range_min = 0
range_max = 0
interval = 0


def player_interval_input():
    error1 = True
    # main while loop, checks if input is in range
    error2 = True
    # while loop is inside of error1, checks if input is an int or float
    while error1:
        interval = input("enter interval between 0 and 3:")
        while error2:
            try:
                interval = float(interval)
                error2 = False
            except ValueError:
                print("invalid input")
        if interval in np.arange(0, 3, 0.25):
            error1 = False
        else:
            print("interval has to be in between 0 and 3, try again")


def min_max_input():
    error3 = True
    # input for min and max range of random numbers
    while error3:
        range_min = input("enter minimum:")
        range_max = input("enter maximum:")
        if range_min.isdigit() or range_max.isdigit():
            range_min = int(range_min)
            range_max = int(range_max)
            error3 = False
        else:
            print("invalid input")


start = input("press 1 to start:")
try:
    start = int(start)
except ValueError:
    print("invalid input, try again")
if start == 1:
    player_interval_input()
    min_max_input()

answer = random.randrange(range_min, range_max)
print("press G when the number shown is " + str(answer))
print()

#main function, runs the guessing program
run = True
while run:
    time.sleep(interval)
    randnum = random.randint(range_min,range_max)
    print(randnum)
Jai Sloper
  • 78
  • 5
0

you could also use global variables but using globals are sort of frowned upon in the python community

#timing game

import random
import time
import numpy as np


def player_interval_input():
    global interval
    error1 = True
    # main while loop, checks if input is in range
    error2 = True
    # while loop is inside of error1, checks if input is an int or float
    while error1:
        interval = input("enter interval between 0 and 3:")
        while error2:
            try:
                interval = float(interval)
                error2 = False
            except ValueError:
                print("invalid input")
        if interval in np.arange(0, 3, 0.25):
            error1 = False
        else:
            print("interval has to be in between 0 and 3, try again")


def min_max_input():
    global range_min, range_max
    error3 = True
    # input for min and max range of random numbers
    while error3:
        range_min = input("enter minimum:")
        range_max = input("enter maximum:")
        if range_min.isdigit() or range_max.isdigit():
            range_min = int(range_min)
            range_max = int(range_max)
            error3 = False
        else:
            print("invalid input")


start = input("press 1 to start:")
try:
    start = int(start)
except ValueError:
    print("invalid input, try again")
if start == 1:
    player_interval_input()
    min_max_input()

answer = random.randrange(range_min, range_max)
print("press G when the number shown is " + str(answer))
print()

#main function, runs the guessing program
run = True
while run:
    time.sleep(interval)
    randnum = random.randint(range_min, range_max)
    print(randnum)
Jai Sloper
  • 78
  • 5