so getBoundedNumber() is to let the user input some numbers and return the input. My other functions base on the return value and do calculations. I tried to assign it to a global variable outside the function(like var=getBoundedNumber(...)),but instead of storing the value from function, it executes the function again and ask for user to input again. How do I fix this?Thank you!
def getBoundedNumber(qq,ww,ee):
while True:
try:
AA = float(input('Enter chain parameter between %r and %r '%(qq,ww)))
ee='%r is not between %r and %r'%(AA,qq,ww)
assert type(AA)==float or int,'not a valid input'
if qq<= AA <= ww:
break
else:
print(ee)
except ValueError:
print(' not a valid input')
exit
return AA
This is my main function:
def MAIN():
while True:
xx=menu()
if xx == 'A':
aa=getBoundedNumber(0.5,4.5,1)
elif xx == 'N':
numpoints=getBoundedNumber(10,100000,1)
elif xx == 'P':
print(' Parameter a:%20.3f'%aa,'[m]')#console gives me aa not defined
print(' Number of points:%20d'%numpoints)#console gives me numpoints not defined
print(' Length:%20.3f'%chainlength(xs,ys),'[m]')#this function requires value from aa and numpoints so it won't execute.
print(' Angle with support:%20.3f'%anglewithsupport(g_,dd,aa,numpoints))
print(' Net force:%20.3f'%netforce(),'[N]')
elif xx == 'Q':
break
else:
print(' not a valid option')
exit
This is my menu function
def menu():
print('Main menu choices')
print(' A - enter chain parameter a' )
print(' N - enter number of points in chain' )
print(' P - calculate and print chain results')
print(' Q - quit program')
while True:
try:
SELECTION = input('Enter your selection: ')
if SELECTION.upper()== 'A':
break
elif SELECTION.upper() == 'N':
break
elif SELECTION.upper()=='P':
break
elif SELECTION.upper()=='Q':
break
else:
print('%r is not a valid option'%SELECTION)
except ValueError:
print('%r is not a valid option'%SELECTION)
exit
return SELECTION.upper()