Fork me on GitHub

Lei Wang's Python Basics

by Lei Wang

24 Jan 2014

by Lei Wang

1.Take the sentence: All work and no play makes Jack a dull boy. Store each word in a separate variable, then print out the sentence on one line using print.

Code:

1
2
3
4
5
6
7
8
9
10
11
12
 
word1 = "All"
word2 = "work"
word3 = "and"
word4 = "no"
word5 = "play"
word6 = "makes"
word7 = "Jack"
word8 = "a"
word9 = "dull"
word10 = "boy."
 
print word1, word2, word3, word4, word5, word6, word7, word8, word9, word10

Output:

1
 
All work and no play makes Jack a dull boy.

2.A program that will compute MPG for a car. Prompt the user to enter the number of miles driven and the number of gallons used. Print a nice message with the answer. For stye points, cue the message printed off of how efficient (or not) the car is.

Code:

1
2
3
4
5
6
7
8
9
10
11
12
 
mile = int(raw_input("Please enter the number of miles driven?"))
gallon = int(raw_input("Please enter the number of gallons used?"))
 
mpg = mile / gallon
 
print "You entered miles: ", mile
print "You entered gallons: ", gallon
print "The mpg: ", mpg
if mpg > 25:
    print "Congratuation, you got a efficient car!"
else:
    print "Your car is not a efficient car!"

Output:

1
2
3
4
 
You entered miles:  150
You entered gallons:  5
The mpg:  30.0
Congratuation, you got a efficient car!

3. A program that will convert a user-input number of degrees celsius to degrees fahrenheit.

Code:

1
2
3
4
5
6
 
deg_c = int(input("What is the temperature in Celsius? "))
 
# formula to convert C to F is: (degrees Celcius) times (9/5) plus (32)
deg_f = deg_c * (9 / 5) + 32
 
print (deg_c, "degrees Celsius is", deg_f, "degrees Farenheit.")

Output:

1
 
15 degrees Celsius is 59 degrees Farenheit.

4.A program that will convert a user-input number of degrees degrees fahrenheit to degrees celsius.

Code:

1
2
3
4
5
 
deg_f = int(input("What is the temperature in Farenheit? "))
 
deg_c= (deg_f - 32)/ 1.8
 
print (deg_f, "degrees Farenheit is", deg_c, "degrees Celsius.")

Output:

1
 
59 degrees Farenheit is 15 degrees Celsius.

5.A program that will convert degrees or celsius as above, depending on the users' choice.

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
flag = raw_input('If convert C to F, please enter "y", if convert F to C, please enter"n".')
if flag == "y":
    deg_c = int(input("What is the temperature in Celsius? "))
 
# formula to convert C to F is: (degrees Celcius) times (9/5) plus (32)
    deg_f = deg_c * (9 / 5) + 32
 
    print deg_c, "degrees Celsius is", deg_f, "degrees Farenheit."
elif flag == "n":
    deg_f = int(input("What is the temperature in Farenheit? "))
 
    deg_c= (deg_f - 32)/ 1.8
 
    print deg_f, "degrees Farenheit is", deg_c, "degrees Celsius."
else :
    print "Wrong Input"

Output:

If you enter y and the temperature in Celsius:

1
 
15 degrees Celsius is 59 degrees Farenheit.

If you enter y and the temperature in Farenheit:

1
 
59 degrees Farenheit is 15 degrees Celsius.

If you enter other characters:

1
 
Wrong Input
Lei Wang is a second year MSIS student. Find Lei Wang on Twitter, Github, and on the web.