Advertisement
Guest User

dvdjaco - chapter 2

a guest
Feb 13th, 2012
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.60 KB | None | 0 0
  1. # Chapter 2 exercises
  2. #
  3. ###
  4. # Exercise 2.2 Write a program that uses raw_input to prompt a user for their name and then welcomes them.
  5. ###
  6. user_name = raw_input('Enter your name: ')
  7. print 'Hello ' + user_name
  8.  
  9. ###
  10. # Exercise 2.3 Write a program to prompt the user for hours and rate per hour to compute gross pay.
  11. ###
  12.  
  13. prompt_hours = 'Please enter the number of hours: '
  14. prompt_rate = 'Please enter the hourly rate: '
  15.  
  16. hours = raw_input(prompt_hours)
  17. rate = raw_input(prompt_rate)
  18.  
  19. gross_pay = float(hours) * float(rate)
  20. print 'The gross pay is ' + str(gross_pay)
  21.  
  22. ###
  23. # Exercise 2.4 Assume that we execute the following assignment statements:
  24. ###
  25. width = 17
  26. height = 12.0
  27.  
  28. # For each of the following expressions, write the value of the expression and the type (of the value of the expression).
  29. # 1. width/2
  30. # --> 8 (int)
  31. # 2. width/2.0
  32. # --> 8.5 (float)
  33. # 3. height/3
  34. # --> 4.0 (float)
  35. # 4. 1 + 2 * 5
  36. # --> 11 (int)
  37. #
  38. # Check:
  39. #
  40. # >>> width/2
  41. # 8
  42. # >>> type(width/2)
  43. # <type 'int'>
  44. # >>> width/2.0
  45. # 8.5
  46. # >>> type(width/2.0)
  47. # <type 'float'>
  48. # >>> height/3
  49. # 4.0
  50. # >>> type(height/3)
  51. # <type 'float'>
  52. # >>> 1 + 2 * 5
  53. # 11
  54. # >>> type (1 + 2 * 5)
  55. # <type 'int'>
  56. # >>>
  57.  
  58. ###
  59. # Exercise 2.5 Write a program which prompts the user for a Celsius temperature, convert the
  60. # temperature to Fahrenheit and print out the converted temperature.
  61. ###
  62.  
  63. prompt = 'Please enter the temperature in Celsius degrees: '
  64. celsius_temp = raw_input(prompt)
  65.  
  66. fahrenheit_temp = float(celsius_temp)*(9.0/5.0) + 32.0
  67.  
  68. print 'The temperature in Fahrenheit degrees is: ' + str(fahrenheit_temp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement