my code needs to ask the user for 3 numbers. if the numbers are over 100
or under 1
, tell them "no way, try a different number"
My problem is: I can't figure out how to define my variable prompt
, and am getting the below stacktrace
when I run my code.
Code:
def get_int(prompt, minval, maxval):
"""gets a value for an input. if its too small or large gives error"""
n= int(input("Choose a number between 1 and 100: "))
maxval= n > 100
minval= n< 1
prompt = n
int_choice.append(n)
return None
int_choice=[]# list for adding inputs
for i in range (3):
get_int(prompt, minval, maxval)
if n== minval or n== maxval:
print("no way, try a diffrent number")
int_choice.append(n)
print("you chose: ", int_choice)
Stacktrace:
>line 18, in <module>
get_int(prompt, minval, maxval)
NameError: name 'prompt' is not defined
is the error message
Here is how I would approach the get_int function:
def get_int(prompt, minval, maxval):
'''Prompt for integer value between minval and maxval, inclusive.
Repeat until user provides a valid integer in range.
'''
while 1:
n = int(input(prompt))
if (n < minval):
print("value too small")
print("value must be at least {0}".format(minval))
elif (n > maxval):
print("value too large")
print("value must be not more than {0}".format(maxval))
else:
print("value accepted")
return n
pass
# TODO: raise a ValueError or a RuntimeError exception
# if user does not provide valid input within a preset number tries
if __name__ == "__main__":
# Example: test the get_int function
# Requires user interaction.
# Expect out-of-range values 0, 101, -5, etc. should be rejected.
# Expect range limit values 1 and 100 shoudl be accepted.
# Expect in-range values like 50 or 75 should be accepted.
minval = 1
maxval = 100
test1 = get_int("Choose a number between {0} and {1}: ".format(
minval,maxval), minval, maxval)
print("get_int returned {0}".format(test1))
Inside function get_int
, the prompt
, minval
, and maxval
arguments are already defined because they are in the argument list. The prompt
argument is passed to the input()
function, and then the minval
and maxval
limits are used for range checking within an infinite while loop. The function returns a valid number within the range. If the user enters an integer that's out of range, we ask them again until they give an acceptable input. So the caller is guaranteed to get an integer within the specified range.
This isn't ideal design, because what if the user doesn't want to enter a number, but they want to "navigate back"... so that's outside the scope of this approach. But there is a more advanced programming technique called exception handling (read up on try
/ catch
/ throw
for examples) 7.4. The try statement.
Outside the function, where get_int
is invoked, the minval
and maxval
are defined as global variables in the main module namespace. For testing I just ran in interactive mode, accepting a single value. Tested on python 2.7 and python 3.2.
If you've never seen the "xxxxx {0} xxxx".format(value)
string formatting expression before, that's described in the python help files section 6.1.2. String Formatting and 6.1.3.2. Format examples.