I'm new to programming, and I'm looking for some advice on what to do. The program I'm trying to write will need several numbers from the user.
I want to use a function to test if the user input a number or not for each of the values entered. If the input isn't a number, I want the function to keep asking for a number until one is entered. I want to know if there is a better way to pass the value into a global variable other than explicitly declaring each variable in the function as global. I'm unsure if there is a better way to do this...
rowNum = None
def numTest(c, d):
x = False
while x is not True:
try:
c = raw_input(d)
c = float(c)
x = True
except:
print "The value you entered isn't a valid number, please try again."
global rowNum
rowNum = c
numTest(rowNum, "Enter number of rows: ")
print rowNum
# I want to use the numTest function on several variables...
# e.g.
# numTest(contourLevel, "Enter number of contour levels: ")
# numTest(cellSize, "Enter cell size: ")
# numTest(labelSize, "Enter label size: ")
Just make it a function, that returns something instead of manipulating a global, much easier to maintain!
def get_num(msg):
while True:
try:
return int(raw_input(msg)) # number of rows should be int
except ValueError:
print "The value you entered isn't a valid number, please try again."
num_rows = get_num("Enter number of rows: ")
print num_rows
Instead of something like numTest(cellSize, "Enter cell size: ")
which you mentioned,
you should be doing cellSize = get_num("Enter cell size: ")
. It's better practice to return values.
You could make this function more general for int
and float
like so:
def get_num(msg, type_=int):
while True:
try:
return type_(raw_input(msg))
except ValueError:
print "The value you entered isn't a valid number, please try again."
now you can also have:
x = get_num("test") # normal int
x = get_num("foo", float)
I don't have enough rep so I can't comment but:
@Schack,
think of return as kind of an internal print statement (you can't see what is returned, but the Python interpreter can read and use/store/manipulate that value.)
The print statement shows the output on the screen (for humans) although does nothing to your program calculations/running, and the return statement sends the output to the python interpreter(for the program) although shows nothing on your screen for humans to read.
if a function returns a string for example, you could store that output as a variable by stating:
variable1 = function1(args)
which will store whatever string function1 returns (based on an input of args) as variable1.
This is incredibly useful for organising code and means you don't have to fiddle with global variables.
An important thing to note is that once a function returns a value, no other code executes.
So for example:
def example(a, b):
for n in range(1, 30):
if a**n == b**4:
return
break
else:
return False
The break and else statement are redundant because after the function executes the return statement, no other code will execute. A better way to write this would be:
def example(a, b):
for n in range(1, 30):
if a**n == b**4:
return n
return False
I hope I haven't confused you at all as I did get a little sidetracked with my answer, just trying to help, as I'm new myself and found this type of explanation useful when I learnt these topics