## Want to see if this can be simplified

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

### Want to see if this can be simplified

Hey guys, new here. Pretty much new to python, just started doing the MIT CS 1 course and got the whole textbook and everything. So there is this "Finger Exercise" at the end of chapter one that I decided to do, which is pretty much code a driving simulation (kind of) between two distances and see how many tickets you can get. Although it was pretty vague, as it left out important details, I decided to just arbitrarily map out my own destination, speed, speed limit, etc. This honestly took me forever; a lot of reading and thinking, but I learned a lot in the process, I'm far ahead than I expected.

I was wondering if any of you guys could take a look at this. Just to kinda grade it and possibly see where I could make things simpler. From what I understand, part of coding is just trying to limit the amount of unnecessary tests, and just optimize things for a better look.

I've learned to keep tabs on errors; just for precautions I have areas labeled so I am 100% sure of where the coding lands. You can see that where I have print('Ticket'), print('A1'), and so on.

Code: Select all
`from random import choiceimport timeDestination = 436480 # 248 milesMDestination = 248MTotal = 0Ticket = 0ALimit = 123200 # 70 milesASLimit = 70AThres = 140800 # 80 milesASThres = 80BLimit = 305600 # 60 milesBSLimit = 60BThres = 323200 # 70 milesBSThres = 70Speed = range(40, 91) # 1 yd to 1 mph = 1760Distance = 0while Distance < Destination:    MD = MDestination - MTotal # This is so driver doesn't go over the destination limit    Velocity = choice(Speed)    if MD <= 90:        if MD >=40:            Speed = range(40, MD+1)            Velocity  = choice(Speed)            print('MD Test 1')        elif MD < 40:            Velocity = 40 - MD            Velocity = 40 - Velocity            print('MD Test 2')    MTotal = MTotal + Velocity    print('Velocity: ' + str(Velocity))    Distance = Distance + (Velocity * 1760)     print('Distance: ' + str(Distance))    if Distance < BLimit:        if Distance >= ALimit and Distance <= AThres:            if Velocity >= ASLimit+5 and Velocity < ASLimit+7:                n = range(1, 5)                Probability = choice(n)                if Probability == 2:                    print('Ticket')                    Ticket = Ticket + 1                    print('A1')            elif Velocity >= ASLimit+7 and Velocity < ASThres+5:                n = range(1, 3)                Probability = choice(n)                if Probability == 1:                    print('Ticket')                    Ticket = Ticket + 1                    print('A2')            elif Velocity >= ASThres+5:                print('Ticket')                Ticket = Ticket + 1                print('A3')    else:        if Distance >= BLimit and Distance <= BThres:            if Velocity >=BSLimit+5 and Velocity < ASLimit+7:                n = range (1, 5)                Probability = choice(n)                if Probability == 2:                    print('Ticket')                    Ticket = Ticket + 1                    print('B1')            elif Velocity >= ASLimit+7 and Velocity < BSThres+5:                n = range(1, 3)                Probability = choice(n)                if Probability == 1:                    print('Ticket')                    Ticket = Ticket + 1                    print('B2')            elif Velocity >= BSThres+5:                print('Ticket')                Ticket = Ticket + 1                print('B3')print('You have ' + str(Ticket) + ' Tickets')if Distance < Destination:    Final = Destination - Distance    Final = int(Final/1760)    print('You have only ' + str(Final) + ' miles to go.')`
AntiMe

Posts: 1
Joined: Tue Aug 06, 2013 5:45 am

### Re: Want to see if this can be simplified

You can start by breaking it up with functions. That'll make it much, much easier to read and comment on.

A lot of us here appreciate PEP 8 adherent code as well.
Join the #python-forum IRC channel on irc.freenode.net for off-topic chat!

Please prefer not to PM members. The point of the forum is so that anyone can benefit. We don't want to help you over PMs/emails/Skype chats that others can't benefit from

micseydel

Posts: 2252
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA