## 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 choice
import time

Destination = 436480 # 248 miles
MDestination = 248
MTotal = 0
Ticket = 0
ALimit = 123200 # 70 miles
ASLimit = 70
AThres = 140800 # 80 miles
ASThres = 80
BLimit = 305600 # 60 miles
BSLimit = 60
BThres = 323200 # 70 miles
BSThres = 70
Speed = range(40, 91) # 1 yd to 1 mph = 1760
Distance = 0
while 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.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

micseydel

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