Simplify Python

J

ja1lbr3ak

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: ")
 
J

ja1lbr3ak

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.
 
J

ja1lbr3ak

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.
 
J

Joaquin Abian

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
 
J

ja1lbr3ak

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.
 
J

Joaquin Abian

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)
 
D

Dave Angel

ja1lbr3ak said:
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: ")
Since this is apparently some form of self-imposed contest, it'd be nice
to know the rules. Usual rule of thumb is to make the program correct
before trying to optimize it. For example, this program doesn't guard
against user error, like entering AAA+5 for the equation, nor against
maliciousness, such as entering range(10**8).

Also, it refers to 'equation' when a better term would be 'expression.'

So, are you trying to minimize line count? Or character count? Or
variables? Or readability (look up obfuscation contest) ?

You could eliminate the redundant parens around the call to input().
You could eliminate the loop variable and the extra call to input() by
using break, and moving the test to the beginning of a while-True loop.
And if you used elif clauses, you wouldn't need the separate if test.
You could probably obfuscate the fibonacci loop by some kind of list
comprehension.
You could simplify processing n by using range() instead of while. Then
no increment is needed.
You could use semicolons to pack multiple statements on each line if
line count matters, and character count does not.
You could use tab for indenting (yecch) if characters matter. And use
one-character variable names (yecch).

DaveA
 
A

AlienBaby

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



?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top