Shorten my code / make it look nice

This is the place for queries that don't fit in any of the other categories.

Shorten my code / make it look nice

Postby vrkiller » Fri Jun 28, 2013 11:09 am

Hello this is my first post, since my old account was deleted (i believe when the "python forum archive" was made)

So yeah... Hi! :D

I'm a bit of a noob to python, i'm looking for a way to make my code be more understandable, maybe to shorten it by using some cool python syntax. The reason why I'm asking
is because i don't know what to look for when I'm searching. My code looks like this (it is functional, so go ahead and try it if you want to ;) )

Code: Select all
import string
def main():
    print("Just fun and games :D")
    RawString = input("Put some mathshit in here dog:")
    n=0

    RawString=RawString.replace("+"," + ")
    RawString=RawString.replace("-"," - ")
    RawString=RawString.replace("*"," * ")
    RawString=RawString.replace("/"," / ")
    RawString=RawString.replace("("," ( ")
    RawString=RawString.replace(")"," ) ")
    calcus=RawString.split(" ")

# gange samme
    x=0
    #for x in x(0, len(calcus)):
    while x<len(calcus):
        if calcus[x]=="*":
            calcus[x]=float(calcus[x-1])*float(calcus[x+1])
            print(calcus)
            del calcus[x+1]
            del calcus[x-1]
            x=0

        if calcus[x]=="/":
            calcus[x]=float(calcus[x-1])/float(calcus[x+1])
            print(calcus)
            del calcus[x+1]
            del calcus[x-1]
            x=0
        x+=1



    x=0
    while x<len(calcus):
        if calcus[x]=="+":
            calcus[x]=float(calcus[x-1])+float(calcus[x+1])
            print(calcus)
            del calcus[x+1]
            del calcus[x-1]
            x=0
        x+=1




        print(calcus)
if __name__ == '__main__':
    main()




as you can see the code has a lot of repetitions and is not very nice looking if i should say so myself. Do any of you have some fancy tricks, that will help the code be more functional + readable and compact? I would love to hear some positive long answers
- VrKiller
User avatar
vrkiller
 
Posts: 19
Joined: Fri Jun 28, 2013 10:50 am
Location: Denmark, århus

Re: Shorten my code / make it look nice

Postby ochichinyezaboombwa » Fri Jun 28, 2013 5:00 pm

1)
vrkiller wrote:it is functional

-- sorry to disappoint you but : no it is not.

2) you have a lot of repetitions, such as:
Code: Select all
    RawString=RawString.replace("+"," + ")
    RawString=RawString.replace("-"," - ")

: you can use python's data structures such as list and for loops to make it shorter and more error-proof, e.g.
Code: Select all
for ch in "+-*/()":
    RawString=RawString.replace(ch," %s " % ch) 

-- although this whole section is not needed for expression parsing (your approach is incorrect);

3) You don't use functions. Every time you see some repetitive section / pattern, it's a candidate for a function;

4) You almost never in your lifetime will need to use del in a Python program.


In short: go through some tutorial. Lists, loops, functions are your friends.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Shorten my code / make it look nice

Postby vrkiller » Fri Jun 28, 2013 6:27 pm

I meant the code was functional as in "readable" i like your suggestions and i will try to look further into that (:
but wh at should i use instead of "del" is there another way to do it that i am missing?
- VrKiller
User avatar
vrkiller
 
Posts: 19
Joined: Fri Jun 28, 2013 10:50 am
Location: Denmark, århus

Re: Shorten my code / make it look nice

Postby ochichinyezaboombwa » Fri Jun 28, 2013 8:59 pm

Don't use anything instead of del : you just don't need it.
The problem you are trying to solve is a classical example for using recursion.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Shorten my code / make it look nice

Postby vrkiller » Sat Jun 29, 2013 12:12 am

But do that mean i should make a whole new code?
- VrKiller
User avatar
vrkiller
 
Posts: 19
Joined: Fri Jun 28, 2013 10:50 am
Location: Denmark, århus


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests

cron