Fork me on GitHub

Python_Basics

by Brendan Ferreri-Hanberry

23 Jan 2014

## Excercise 4 ##

# I know this one was not required, but you did suggest learning how to 
# convert the numbers into days for bonus points, so I did.

start_day = int(input("What day are you leaving on, if 0 is Sunday and 6 is Saturday?"))
stay_length = int(input("In how many days will you return?"))
answer = ((start_day + stay_length) % 7)
if answer == 0:
    day_name = "Sunday"
elif answer == 1:
    day_name = "Monday"
elif answer == 2:
    day_name = "Tuesday"
elif answer == 3:
    day_name = "Wednesday"
elif answer == 4: 
    day_name = "Thursday"
elif answer == 5:
    day_name = "Friday"
else: day_name = "Saturday"
print("When you return, it will be %s." % day_name)

## Excercise 5 ##

a = "All"
b = "work"
c = "and"
d = "no"
e = "play"
f = "makes"
g = "Jack"
h = "a"
i = "dull"
j = "boy"
print(a,b,c,d,e,f,g,h,i, “%s.” % j)

# Mention was made in class of a "split" function, which can do a similar thing; you can see it below.

str = "All work and no play makes Jack a dull boy."
print str.split()

## Excercise 10 ##

miles = float(input("How many miles did you drive on your last tank of gas?"))
gallons = float(input("How many gallons did you need when you went to the gas station afterwards?"))
mpg = int(miles / gallons)
print("You are getting about", mpg, "miles per gallon.")
if mpg > 24.9:
    print("This is better than the average for a new car in 2013.")
elif mpg < 24.9:
    print("This is lower than the average for a new car in 2013.")
else:
    print("This is the average for a new car in 2013.")

## Excercise 11 ##

C = float(input("What is the temperature in degrees Celsius?"))
F = int(C * 9 / 5 + 32)
print(C, "degrees Celsius is equivalent to about", F, "degrees Fahrenheit.")

## Excercise 12 ## 

F = float(input("What is the temperature in degrees Fahrenheit?"))
C = int((F - 32) * 5 / 9)
print(F, "degrees Fahrenheit is equivalent to about", C, "degrees Celsius.")

## Excercise 13 ##

# This is not an actual exercise number,
# but all the previous ones listed in the assignment under "Python Basics"
# had numbers listed on the Chapter 2 readings page,
# so I am giving this one a number so as not to make it feel lonely.

def ftoc():
    F = float(input("What is the temperature in degrees Fahrenheit?"))
    C = int((F - 32) * 5 / 9)
    print(F, "degrees Fahrenheit is equivalent to about", C, "degrees Celsius.")

def ctof():
    C = float(input("What is the temperature in degrees Celsius?"))
    F = int(C * 9 / 5 + 32)
    print(C, "degrees Celsius is equivalent to about", F, "degrees Fahrenheit.")

choice = raw_input("This program can convert Celsius to Fahrenheit or Fahrenheit to Celsius. Which would you like to convert from, Celsius or Fahrenheit?")
if choice == "Celsius":
    ctof()
elif choice == "celsius":
    ctof()
elif choice == "C":
    ctof()
elif choice == "c":
    ctof()
elif choice == "Fahrenheit":
    ftoc()
elif choice == "fahrenheit":
    ftoc()
elif choice == "F":
    ftoc()
elif choice == "f":
    ftoc()
else:
    print("Sorry, that was not one of your options.")

You ask for "A pull request to our class blog with the code above and sample output from your programs." It is not clear where you want the sample output to be or how much you need, so I am placing one example for each exercise (except Exercise 5, where I am providing two) here.

From Exercise 4: When you return, it will be Saturday.

Exercise 5: All work and no play makes Jack a dull boy.

Exercise 5 with the "split" command: ['All', 'work', 'and', 'no', 'play', 'makes', 'Jack', 'a', 'dull', 'boy.']

Exercise 10: You are getting about 20 miles per gallon. This is lower than the average for a new car in 2013.

Exercise 11: 45 degrees Celsius is equivalent to about 113 degrees Fahrenheit.

Exercise 12: 22 degress Fahrenheit is equivalent to about -5 degrees Celsius.

Exercise 13: -9 degrees Celsius is equivalent to about 15 degrees Fahrenheit.

Brendan is an MSIS student from Virginia. In his free time, he writes articles about drug policy at ssdp.org/about/blog-contributors/brendan-ferreri-hanberry/ and at the-libertarian.co.uk, where he serves as an editor. Find Brendan Ferreri-Hanberry on Twitter, Github, and on the web.
comments powered by Disqus