Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Chapter 2 exercises
- #
- ###
- # Exercise 2.2 Write a program that uses raw_input to prompt a user for their name and then welcomes them.
- ###
- user_name = raw_input('Enter your name: ')
- print 'Hello ' + user_name
- ###
- # Exercise 2.3 Write a program to prompt the user for hours and rate per hour to compute gross pay.
- ###
- prompt_hours = 'Please enter the number of hours: '
- prompt_rate = 'Please enter the hourly rate: '
- hours = raw_input(prompt_hours)
- rate = raw_input(prompt_rate)
- gross_pay = float(hours) * float(rate)
- print 'The gross pay is ' + str(gross_pay)
- ###
- # Exercise 2.4 Assume that we execute the following assignment statements:
- ###
- width = 17
- height = 12.0
- # For each of the following expressions, write the value of the expression and the type (of the value of the expression).
- # 1. width/2
- # --> 8 (int)
- # 2. width/2.0
- # --> 8.5 (float)
- # 3. height/3
- # --> 4.0 (float)
- # 4. 1 + 2 * 5
- # --> 11 (int)
- #
- # Check:
- #
- # >>> width/2
- # 8
- # >>> type(width/2)
- # <type 'int'>
- # >>> width/2.0
- # 8.5
- # >>> type(width/2.0)
- # <type 'float'>
- # >>> height/3
- # 4.0
- # >>> type(height/3)
- # <type 'float'>
- # >>> 1 + 2 * 5
- # 11
- # >>> type (1 + 2 * 5)
- # <type 'int'>
- # >>>
- ###
- # Exercise 2.5 Write a program which prompts the user for a Celsius temperature, convert the
- # temperature to Fahrenheit and print out the converted temperature.
- ###
- prompt = 'Please enter the temperature in Celsius degrees: '
- celsius_temp = raw_input(prompt)
- fahrenheit_temp = float(celsius_temp)*(9.0/5.0) + 32.0
- print 'The temperature in Fahrenheit degrees is: ' + str(fahrenheit_temp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement