-5

I'm working on a program at school and got into this problem that really screwed around with me, probably because I'm rather nooby at Python and Programming itself.

Anyway, here is the section of code that caused the problem:

# A function used to determine a 'try' and 'except' loop for an input of a integer data type. It also adds boundaries for the integer variables in this program        
def tryAndExceptInputInt(text):
while True:
    try:
        variable = int(input(text))
        if variable == speedLimit:
            if variable > 120 or variable < 5:
                raise ValueError("Please input a suitable speed limit")
            else:
                break
        elif variable == distance:
            if variable < 1:
                raise ValueError("Please input a suitable distance")
            else:
                break
        elif variable == vehicles:
            if variable <= 0:
                raise ValueError("Please input a valid number of vehicle(s)")
            else:
                break  
        elif variable == time1 or variable == time2:
            if len(variable) != 4:
                raise ValueError("Please input a suitable entrance/leaving time")
            else:
                variable = int(variable)
                if variable >= 2400 or variable < 0000:
                    raise ValueError("Please input a suitable entrance/leaving time")
                else:
                    break
        else:
            break
    # This statement keeps looping until the user inputs the suitable data type
    except ValueError:
        print("Please input a value which is an integer\n")
    else:
        return variable

# MAIN PROGRAM


# A function made to loop 'try' and 'except' so that the user's input would be the desired data type as well as variable
speedLimit = tryAndExceptInputInt("Enter the speed limit in the monitored area (mph): ")

Here is the Error Message I received for running this part of code:

"In 'tryAndExceptInputInt', 'if variable == speedLimit:', NameError: name 'speedLimit' is not defined

Can someone answer me with a correct version of the 'tryAndExceptInputInt' function I created, Thanks ;-)

Metz Lush
  • 11
  • 2
  • Well, indeed, it is not defined. Where do you think you have defined it? Where do you want it to come from? – Daniel Roseman Mar 08 '16 at 18:20
  • I want it to come from the user as an input. – Metz Lush Mar 08 '16 at 18:23
  • No, that's "variable", the value you're comparing. Where do you want the variable "speedLimit" to come from? – Daniel Roseman Mar 08 '16 at 18:24
  • 1
    I think you want a `kind` perameter instead of using `variable==speedLimit` but in any case why the heck are you using one function that raises value errors with useful messages to just throw them away and print `Please input a value which is an integer`, I'm not sure you understand the code you wrote. – Tadhg McDonald-Jensen Mar 08 '16 at 18:26
  • @DanielRoseman, it is defined as the return value of the function that requires it defined already – Tadhg McDonald-Jensen Mar 08 '16 at 18:26
  • I want it set to an execution to the function I created, which is at the all the way at the bottom of the code. – Metz Lush Mar 08 '16 at 18:28
  • 3
    Well, that doesn't make sense, does it? You can't use the return value of a function inside the function that is supposed to be calculating and returning it. – Daniel Roseman Mar 08 '16 at 18:29
  • I probably sound like a noob, don't I? :( – Metz Lush Mar 08 '16 at 18:30
  • 6
    Everyone's new once, that is not a problem. The trick is to think logically about what you want to do; the rest is just syntax. So, think about what actions you want your function to do, and what information it will need to do it. – Daniel Roseman Mar 08 '16 at 18:33
  • 1
    Thanks a lot for the advice, Daniel Roseman, I only started this year XD – Metz Lush Mar 08 '16 at 18:34

1 Answers1

-7

get rid of speedLimit = tryAndExceptInputInt("Enter the speed limit in the monitored area (mph): ") and replace with

speed = input("speedLimit = tryAndExceptInputInt("Speed (MPH): ")
tryAndExceptInputInt(int(speed))

and replace def tryAndExceptInputInt(text): with

def tryAndExceptInputInt(speedLimit):

  • 1
    this would cover up the original error but would not fix anything. Did you try actually running the code with your suggestion? **It's not valid python syntax.** – Tadhg McDonald-Jensen Mar 08 '16 at 18:33
  • 3
    you will get a syntax error, after you fix that you will get another name error, if you fix that you will get a type error, trust me it is not worth testing – Tadhg McDonald-Jensen Mar 08 '16 at 18:37