Simplify Python

ja1lbr3ak superherocomic at gmail.com
Tue Apr 6 16:49:44 EDT 2010


On Apr 6, 4:17 pm, Christopher Choi <chu... at gmail.com> wrote:
> On Tue, 06 Apr 2010 13:14:44 -0700, ja1lbr3ak wrote:
> > On Apr 6, 4:06 pm, Christopher Choi <chu... at gmail.com> wrote:
> >> On Tue, 06 Apr 2010 12:04:20 -0700, ja1lbr3ak 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: ")
>
> >> I'm fairly confused here.
>
> >> But I would do this instead:
>
> >> UserInput1 = input("Enter 1 for the calculator, 2 for the Fibonacci
> >> sequence, or something else to quit: ")
>
> >> While quit = 0:
> >>         if UserInput1 == 1:
> >>                 print input("\nPut in an equation: ")
> >>         else if UserInput1 == 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
> >>                         UserInput2 = input("\nEnter 1 for
> >>                         the calculator,
> >> 2 for the Fibonacci sequence, or something else to quit: ")
>
> >>         else
> >>                 quit = 1
>
> >> the above is not finished... but without actually knowing what your
> >> trying to do.. the above code I just did would make a lot more sense..
> >> I hope... You hit a wall cause the first while loop you had before
> >> never ends..
>
> > The while loop is my main program loop. There at the end is the "loop =
> > input" part again. If you type in something other than 1 or 2, it exits
> > the while loop and ends the program. I also merged the "quit" and
> > "UserInput1" variables in your program into one "loop" variable.
>
> Can you post it please?

It's what I posted before, but I'll add some documentation.

loop = input("Enter 1 for the calculator, 2 for the Fibonacci
sequence, or something else to quit: ") #sets what loop is equal to
while loop < 3 and loop > 0: #if the input is less than 3 and greater
than 0, it continues. otherwise, it exits
    if loop == 1: #it has to be 1 or 2 because of the last line, so if
it's 1...
        print input("\nPut in an equation: ") #it asks for an
equation, does it, then prints the result
    if loop == 2: #if it's 2...
        a, b, n = 1, 1, (input("\nWhat Fibonacci number do you want to
go to? ")) #it sets a + b to 1, and n to the input
        while n > 0: #for as long as n is greater than 0
            print a #it prints a
            a, b, n = b, a+b, n-1 #then sets a = b, b = a+b, and n =
n-1
    loop = input("\nEnter 1 for the calculator, 2 for the Fibonacci
sequence, or something else to quit: ") #finally, it asks for the
input again. if it's 1 or 2, the loop starts over. otherwise, it
exits.



More information about the Python-list mailing list