scruff
December 19th, 2007, 03:02 AM
What's the deal here...
(simplified example)
def main();
rel_type = get_release_type()
def get_release_type():
types = {'1': 'Gold/',
'2': 'QFE/',
'3': 'beta/'}
ans = raw_input("\nWhat type of release is this?\n\n"
"1 = gold\n"
"2 = QFE\n"
"3 = beta\n# ")
if not types.has_key(ans):
print "\nYou must enter either 1, 2, or 3"
get_release_type()
return types[ans]
if __name__ == '__main__':
main()
You can see that get_release_type() will call itself if the user enters invalid input. The strange issue I am seeing is if you DO enter invalid input, the 2nd time around at return types[ans] it will try to look up whatever you invalid key you entered the first time around and raise a KeyError exception.
Stepping through this code in the debugger is further confusing because I can plainly see that ans contains my latest answer, in my case a '1'.
What type of release is this?
1 = gold
2 = QFE
3 = beta
# q
You must enter either 1, 2, or 3
What type of release is this?
1 = gold
2 = QFE
3 = beta
# 1
Traceback (most recent call last):
File "/home/ssulliva/work/test/releaseEngineering/publishRelease/publishRelease.py", line 289, in <module>
main()
File "/home/ssulliva/work/test/releaseEngineering/publishRelease/publishRelease.py", line 168, in main
rel_type = get_release_type()
File "/home/ssulliva/work/test/releaseEngineering/publishRelease/publishRelease.py", line 227, in get_release_type
return types[ans]
KeyError: 'q'
I am relatively new to Python coming from Perl. Any ideas?
(simplified example)
def main();
rel_type = get_release_type()
def get_release_type():
types = {'1': 'Gold/',
'2': 'QFE/',
'3': 'beta/'}
ans = raw_input("\nWhat type of release is this?\n\n"
"1 = gold\n"
"2 = QFE\n"
"3 = beta\n# ")
if not types.has_key(ans):
print "\nYou must enter either 1, 2, or 3"
get_release_type()
return types[ans]
if __name__ == '__main__':
main()
You can see that get_release_type() will call itself if the user enters invalid input. The strange issue I am seeing is if you DO enter invalid input, the 2nd time around at return types[ans] it will try to look up whatever you invalid key you entered the first time around and raise a KeyError exception.
Stepping through this code in the debugger is further confusing because I can plainly see that ans contains my latest answer, in my case a '1'.
What type of release is this?
1 = gold
2 = QFE
3 = beta
# q
You must enter either 1, 2, or 3
What type of release is this?
1 = gold
2 = QFE
3 = beta
# 1
Traceback (most recent call last):
File "/home/ssulliva/work/test/releaseEngineering/publishRelease/publishRelease.py", line 289, in <module>
main()
File "/home/ssulliva/work/test/releaseEngineering/publishRelease/publishRelease.py", line 168, in main
rel_type = get_release_type()
File "/home/ssulliva/work/test/releaseEngineering/publishRelease/publishRelease.py", line 227, in get_release_type
return types[ans]
KeyError: 'q'
I am relatively new to Python coming from Perl. Any ideas?