CMPT165-Python Examples.docx

80 views3 pages
# 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()
Unlock document

This preview shows page 1 of the document.
Unlock all 3 pages and 3 million more documents.

Already have an account? Log in

Get OneClass Grade+

Unlimited access to all notes and study guides.

Grade+All Inclusive
$10 USD/m
You will be charged $120 USD upfront and auto renewed at the end of each cycle. You may cancel anytime under Payment Settings. For more information, see our Terms and Privacy.
Payments are encrypted using 256-bit SSL. Powered by Stripe.