Study Guides (390,000)
CA (150,000)
SFU (5,000)
CMPT (100)
Final

CMPT165-Python Examples.docx


Department
Computing Science
Course Code
CMPT 165
Professor
Colin Stewart
Study Guide
Final

This preview shows half of the first page. to view the full 3 pages of the document.
# QUESTION 1
Write a program that asks the user to type in two decimal (float) numbers. Then print the sum,
difference, product and quotient of these numbers.
num1 = float(raw_input("Enter a number:"))
num2 = float(raw_input("Enter another number:"))
print str(num1) + " + " + str(num2) + " = " + str(num1+num2)
print str(num1) + " - " + str(num2) + " = " + str(num1-num2)
print str(num1) + " * " + str(num2) + " = " + str(num1*num2)
print str(num1) + " / " + str(num2) + " = " + str(num1/num2)
# OR ...
num1 = float()
num2 = float()
print num1, "+", num2, "=", num1+num2
print num1, "-", num2, "=", num1-num2
print num1, "*", num2, "=", num1*num2
print num1, "/", num2, "=", num1/num2
# QUESTION 2
Write a program that asks the user for their name and weight. Tell the user if you’re using
pounds or kilograms. Then print a message like this: "Hi, name, you weigh weight kilograms!"
(where name and weight are the user's inputs).
name = raw_input("Enter your name:")
weight = raw_input("Enter your weight in kilograms:")
print "Hi, " + name + ", you weigh " + weight + " kilograms!"
# QUESTION 3
Write a program that asks the user to enter their first name. Then display the following:
a. the number of characters in their name
b. the first and last character in their name
c. their name in all upper case
d. their name in all lower case
e. each character in their name, separated by a space
name = raw_input("Enter your first name:")
print "You have " + str(len(name)) + " characters in your name."
print "The first character in your name is " + name[0]
print "The last character in your name is " + name[len(name)-1]
print "Your name in all upper case: " + name.upper()
print "Your name in all lower case: " + name.lower()
You're Reading a Preview

Unlock to view full version