Simplify Python

AlienBaby matt.j.warren at gmail.com
Wed Apr 7 17:54:28 CEST 2010


On 6 Apr, 20:04, ja1lbr3ak <superheroco... at gmail.com> wrote:
> I'm trying to teach myself Python, and so have been simplifying a
> calculator program that I wrote. The original was 77 lines for the
> same functionality. Problem is, I've hit a wall. Can anyone help?
>
> loop = input("Enter 1 for the calculator, 2 for the Fibonacci
> sequence, or something else to quit: ")
> while loop < 3 and loop > 0:
>     if loop == 1:
>         print input("\nPut in an equation: ")
>     if loop == 2:
>         a, b, n = 1, 1, (input("\nWhat Fibonacci number do you want to
> go to? "))
>         while n > 0:
>             print a
>             a, b, n = b, a+b, n-1
>     loop = input("\nEnter 1 for the calculator, 2 for the Fibonacci
> sequence, or something else to quit: ")


To replicate what you have above, I would do something like;

quit=False
while not quit:
	choice=input('1 for calc, 2 for fib, any other to quit')
	if choice==1:
		print input('Enter expression: ')
	elif choice==2:
		a, b, n = 1, 1, input("\nWhat Fibonacci number do you want to go to?
")
        	         while n > 0:
            		print a
            		a, b, n = b, a+b, n-1
	else:
		quit=True



?



More information about the Python-list mailing list