Reputation: 103
The getvalidint function is explained below, as I am calling the function getvalidint and giving it a input so it may produce an integer output. It is not as I print the functions output(see below in the main program) it prints out "none", I am running on python33.
#getValidInt() takes in a minn and maxx, and gets a number from the
# user between those two numbers (inclusive)
#Input: minn and maxx, two integers
#Output: an integer, between minn and maxx inclusive
MIN_VAL = -1000000
MAX_VAL = 1000000
def getValidInt(minn, maxx):
message = "Please enter a number between " + str(minn) + " and " + \
str(maxx) + " (inclusive): "
newInt = int(input(message))
while newInt <= minn & newInt >= maxx:
# while loop exited, return the user's choice
return newInt
def main():
userNum = getValidInt(MIN_VAL, MAX_VAL)
print(userNum)
main()
Upvotes: 0
Views: 73
Reputation: 4681
If the while newInt <= minn & newInt >= maxx:
condition is never met, then nothing will be returned. This means that the function will implicitly return None
. Also, assuming you're using python 3 (which I induced from your int(input())
idiom).
The deeper issue is that the input code will only run once, regardless of whether or not the value meets the constraint. The typical way of doing this would be something along the lines of:
import sys
def get_int(minimum=-100000, maximum=100000):
user_input = float("inf")
while user_input > maximum or user_input < minimum:
try:
user_input = int(input("Enter a number between {} and {}: ".format(minimum, maximum)))
except ValueError:
sys.stdout.write("Invalid number. ")
return user_input
Upvotes: 2