Fork me on GitHub

Katie's Basic Python Exercises

by Katie Meyer

22 Jan 2014

String parsing program

Code

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

for word in words:
    sentence = sentence + word + ' '

print sentence

Sample output

String output

MPG program

Code

miles = int(raw_input('How many miles did you drive?'))
gallons = int(raw_input('How many gallons did your car need to go %d miles?' %miles))
mpg = miles / gallons

print 'Your MPG is', mpg, 'miles per gallon.'
if mpg >= 20:
    print 'Good for you!'
if mpg < 20:
    print 'You might want to look into getting a hybrid...'

Sample input

400 miles 20 gallons

Sample output

MPG output

Fahrenheit program

Code

celsius = int(raw_input("What's the temperature in Celsius?"))
fahrenheit = (celsius * 9/5) + 32

print "That's", fahrenheit, "degrees Fahrenheit!"

Sample input

26 degrees

Sample output

Fahrenheit output

Celsius program

Code

fahrenheit = int(raw_input("What's the temperature in Fahrenheit?"))
celsius = (fahrenheit - 32) * 5/9

print "That's", celsius, "degrees in Celsius!"

Sample input

26 degrees

Sample output

Celsius output

Fahrenheit or Celsius program

Code

choice = raw_input("Do you want to convert from Celsius or Fahrenheit?")
if choice == 'Celsius':
    celsius = int(raw_input("What's the temperature in Celsius?"))
    fahrenheit = (celsius * 9/5) + 32
    print "That's", fahrenheit, "degrees Fahrenheit!"
elif choice == 'Fahrenheit':
    fahrenheit = int(raw_input("What's the temperature in Fahrenheit?"))
    celsius = (fahrenheit - 32) * 5/9
    print "That's", celsius, "degrees in Celsius!"
else:
    print("You have to pick Celsius or Fahrenheit!")

Sample input 1

Fahrenheit 50

Sample output 1

Celsius or Fahrenheit output

Sample input 2

Celsius 10

Sample output 2

That's 50 degrees Fahrenheit!

Sample input 3

Blueberry

Sample output 3

You have to pick Celsius or Fahrenheit!
Katie is a first year MSIS student from Raleigh NC. She's also a Web UI Developer at IBM. Find Katie Meyer on Twitter, Github, and on the web.
comments powered by Disqus