[Tutor] Just a newbie question...

Python Newbie python_list@edsamail.com.ph
Fri, 22 Feb 2002 17:01:36 -0800


Hello Python gurus,

Please bear with me, I'm still a 4 days old newbie with Python.  I hope I don't waste your time reading with this very newbie question.  =)

What's wrong with my code?  Why is it that my "x" in fib(x) function is not recognized?

- - - < s n i p > - - -
import sys

print '\n'
print "[ 1 ] The Fibonacci Sequence"
print "[ 2 ] The Multiplication Table"
print "[Q/q] E X I T"
print '\n'
choice=raw_input("Enter your choice: ")

while choice != 'Q' and choice != 'q':
	if choice == 1:
		fib(x)
	elif choice == 2:
		mtable()
	else:
		break

def fib(x):
	x=input("Enter an integer: ")
	a=b=x
	if x > 0:
		print '\n'
		print "The Fibonacci Sequence:", '\n'
		for x in range(10):
			print '\t', a, '\n'
			a, b = b, a+b
fib(x)
		
def mtable():
	print '\n'
	print '\t', '\t', '\t', "The Multiplication Table:", '\n'
	m=1
	while m <= 10:
		def mult(n):
			y=1
			while y<=10:
				print n*y, '\t',
				y=y+1
			print
		mult(m)
		m = m+1
mtable()
- - - < s n i p > - - -

Also, is there a nice way to change this lousy code below?  I want to change it like a "switch" conditional statement in C, to display a "menu-like" choices.  What is a good alternative to "switch" of C in Python?  And what is a "good terminator" if I press "Q or q" in Python?

- - - < s n i p > - - -
print '\n'
print "[ 1 ] The Fibonacci Sequence"
print "[ 2 ] The Multiplication Table"
print "[Q/q] E X I T"
print '\n'
choice=raw_input("Enter your choice: ")

while choice != 'Q' and choice != 'q':
	if choice == 1:
		fib(x)
	elif choice == 2:
		mtable()
	else:
		break
- - - < s n i p > - - -

And last but not the least, how can I test an input key, for example in the "Enter your choice:" located above (my menu), to accept only the particular choices (1, 2, Q or q only)?  If the input key is not in the choices especially if a user inputs a character or string, it will pop an error message.

Thank you very much!

Just a newbie...

__________________________________
www.edsamail.com