[Tutor] Just a newbie question...

Kelly, Phelim KellyPhe@logica.com
Thu, 21 Feb 2002 17:15:59 -0000


Hi

You're trying to send x off to fib(x), but x hasn't been defined yet, you
have your 
"x=input("Enter an integer: ")" in the wrong place, you should have it like
so:

----------------
while choice != 'Q' and choice != 'q':
	
      x=input("Enter an integer: ")

      if choice == 1:
		fib(x)
	elif choice == 2:
		mtable()
	else:
		break

def fib(x):
	
	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
--------------------

That will fix that particular problem anyway!

Phelim.


-----Original Message-----
From: Python Newbie [mailto:python_list@edsamail.com.ph]
Sent: 23 February 2002 01:02
To: Python Tutor Mailing List
Subject: [Tutor] Just a newbie question...


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

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

This e-mail and any attachment is for authorised use by the intended recipient(s) only.  It may contain proprietary material, confidential information and/or be subject to legal privilege.  It should not be copied, disclosed to, retained or used by, any other party.  If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender.  Thank you.