# Simplify Python

Joaquin Abian gatoygata2 at gmail.com
Tue Apr 6 23:41:47 CEST 2010

```On Apr 6, 11:04 pm, ja1lbr3ak <superheroco... at gmail.com> wrote:
> On Apr 6, 4:56 pm, Joaquin Abian <gatoyga... at gmail.com> wrote:
>
>
>
> > On Apr 6, 9:04 pm, 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: ")
>
> > You could structure it a bit better but something to start with could
> > be(it works):
>
> > def calc(n):
> >     print n
>
> > def fibo(n):
> >     a=b=1
> >     while n > 0:
> >         print a
> >         a, b, n = b, a+b, n-1
>
> > NOTE = """Enter 1 for the calculator, 2 for the Fibonacci
> > sequence, or something else to quit: """
>
> > while True:
>
> >     loop = raw_input(NOTE)
>
> >     if loop == '1':
> >         forcalc = raw_input("\nPut in an equation: ")
> >         calc(forcalc)
> >     elif loop == '2':
> >         forfibo = raw_input("\nWhat Fibonacci number do you want to go
> > to? ")
> >         n = int(forfibo)
> >         fibo(n)
> >     else:
> >         break
>
> > Cheers
> > Joaquin
>
> Actually, when I tried yours the calculator just spits out the input
> again, which is why I used input instead of raw_input.

I see. input evaluates your input string while raw_input just yields
the string as a string. I tried to encapsulate the calculator in the
function calc in order for you to implement the logic, making no
assumption about how the calculator should work.
If what you actually want to do is to evaluate strings containing
valid python code you can either use input for the 'Put in an
equation' line or better modify function calc to do the job:

def calc(n):
print eval(n)

```

More information about the Python-list mailing list