Sunday, October 8, 2017

Peer exercise 3

Peer exercise three involved something we haven't covered yet: an if statement. That was my mistake, but I wanted to show you what it would look like."

Here is one way to code that exercise.

# this program uses integer division
#to determine how many vans
#it will take to transport several people
#steven conger 10/8/2017

def main():
    passengers=29
    vanCapacity=8
    print ("For our trip we will have ", passengers, "passengers.")
    print ("Each van can take", vanCapacity, "of them")
    vans = passengers // vanCapacity #integer division
    print (vans, " Will be full.")
    
    extra = passengers % vanCapacity #modulus
      

    #This part requires and if something we really
    #haven't covered yet
    if extra != 0:
        vans=vans + 1
        print ("One van will have ", extra, "passengers")

    print("You will need ", vans, " vans.")
    

main()
    

Here is the result screen

>>> 
== RESTART: C:/Users/steve_conger/OneDrive/Documents/PythonPrograms/vans.py ==
For our trip we will have  29 passengers.
Each van can take 8 of them
3  Will be full.
One van will have  5 passengers
You will need  4  vans.
>>> 

No comments:

Post a Comment