1. # Practice Exercises for Functions
  2. # Solve each of the practice exercises below.
  3. # 1.Write a Python function miles_to_feet that takes a parameter miles and
  4. # returns the number of feet in miles miles.
  5. def miles_to_feet(miles):
  6. feet = miles * 5280
  7. return feet
  8. print(miles_to_feet(2.5))
  9. print('=====')
  10. # 2.Write a Python function total_seconds that takes three parameters hours, minutes and seconds and
  11. # returns the total number of seconds for hours hours, minutes minutes and seconds seconds.
  12. def total_seconds(hours, minutes, seconds):
  13. total = hours * 60 * 60 + minutes * 60 + seconds
  14. return total
  15. print(total_seconds(1, 5, 10))
  16. print('=====')
  17. # 3.Write a Python function rectangle_perimeter that takes two parameters width and height
  18. # corresponding to the lengths of the sides of a rectangle and
  19. # returns the perimeter of the rectangle in inches.
  20. def rectangle_perimeter(width, height):
  21. perimeter = (width + height) * 2
  22. return perimeter
  23. print(rectangle_perimeter(2.3, 2.2))
  24. print('=====')
  25. # 4.Write a Python function rectangle_area that takes two parameters width and height
  26. # corresponding to the lengths of the sides of a rectangle and
  27. # returns the area of the rectangle in square inches.
  28. def rectangle_area(width, height):
  29. area = width * height
  30. return area
  31. print(rectangle_area(2, 5))
  32. print('=====')
  33. # 5.Write a Python function circle_circumference that takes a single parameter radius
  34. # corresponding to the radius of a circle in inches and
  35. # returns the the circumference of a circle with radius radius in inches.
  36. # Do not use π=3.14, instead use the math module to supply a higher-precision approximation to π.
  37. import math
  38. def circle_circumference(radius):
  39. circumference = 2.0 * radius * math.pi
  40. return circumference
  41. print(circle_circumference(4.0))
  42. print('=====')
  43. # 6.Write a Python function circle_area that takes a single parameter radius
  44. # corresponding to the radius of a circle in inches and
  45. # returns the the area of a circle with radius radius in square inches.
  46. # Do not use π=3.14, instead use the math module to supply a higher-precision approximation to π.
  47. def circle_area(radius):
  48. area = radius * radius * math.pi
  49. return area
  50. print(circle_area(4.0))
  51. print('=====')
  52. # 7.Write a Python function future_value that takes three parameters present_value, annual_rate and years and
  53. # returns the future value of present_value dollars invested at annual_rate percent interest,
  54. # compounded annually for years years.
  55. def future_value(present_value, annual_rate, years):
  56. value = present_value * pow(annual_rate + 1.0, years)
  57. return value
  58. print(future_value(1000000.0, 0.03, 10))
  59. print('=====')
  60. # 8.Write a Python function name_tag that takes as input the parameters first_name and last_name (strings) and
  61. # returns a string of the form "My name is % %." where the percents are the strings first_name and last_name.
  62. # Reference the test cases in the provided template for an exact description of
  63. # the format of the returned string.
  64. def name_tag(first_name, last_name):
  65. form = "My name is %s %s." % (first_name, last_name)
  66. return form
  67. print(name_tag('Bob', 'Smith'))
  68. print('=====')
  69. # 9.Write a Python function name_and_age that takes as input the parameters name (a string) and age (a number) and
  70. # returns a string of the form "% is % years old." where the percents are the string forms of name and age.
  71. # Reference the test cases in the provided template for an exact description of
  72. # the format of the returned string.
  73. def name_and_age(name, age):
  74. form = "%s is %d years old." % (name, age)
  75. return form
  76. print(name_and_age('John', 24))
  77. print('=====')
  78. # 10.Write a Python function point_distance that takes as the parameters x0, y0, x1 and y1, and
  79. # returns the distance between the points (x0,y0) and (x1,y1).
  80. def point_distance(x0, y0, x1, y1):
  81. distance = math.sqrt((x0 - x1)**2 + (y0 - y1)**2)
  82. return distance
  83. print(point_distance(0, 0.5, -2.2, 3.5))
  84. print('=====')
  85. # 11.Challenge: Write a Python function triangle_area that takes the parameters x0, y0, x1,y1, x2, and y2, and
  86. # returns the area of the triangle with vertices (x0,y0), (x1,y1) and (x2,y2).
  87. # (Hint: use the function point_distance as a helper function and apply Heron's formula.)
  88. def triangle_area(x0, y0, x1, y1, x2, y2):
  89. side1 = point_distance(x0, y0, x1, y1)
  90. side2 = point_distance(x1, y1, x2, y2)
  91. side3 = point_distance(x2, y2, x0, y0)
  92. area = heron_formula(side1, side2, side3)
  93. return area
  94. # 海伦公式
  95. def heron_formula(side1, side2, side3):
  96. p = (side1 + side2 + side3) / 2.0
  97. area = math.sqrt(p * (p - side1) * (p - side2) * (p - side3))
  98. return area
  99. print(triangle_area(0, 0.5, -2.2, 3.5, -3, -2.5))
  100. print('=====')
  101. # 12.Challenge: Write a Python function print_digits that takes an integer number in the range [0,100),
  102. # i.e., at least 0, but less than 100. It prints the message "The tens digit is %, and the ones digit is %.",
  103. # where the percent signs should be replaced with the appropriate values.
  104. # (Hint: Use the arithmetic operators for integer division // and remainder % to find the two digits.
  105. # Note that this function should print the desired message, rather than returning it as a string.
  106. def print_digits(number):
  107. tens, ones = number // 10, number % 10
  108. message = "The tens digit is %d, and the ones digit is %d." % (tens, ones)
  109. print(message)
  110. print_digits(49)
  111. print('=====')
  112. # 13.Challenge: Powerball is lottery game in which 6 numbers are drawn at random.
  113. # Players can purchase a lottery ticket with a specific number combination and,
  114. # if the number on the ticket matches the numbers generated in a random drawing,
  115. # the player wins a massive jackpot. Write a Python function powerball that takes no arguments and
  116. # prints the message "Today's numbers are %, %, %, %, and %. The Powerball number is %.".
  117. # The first five numbers should be random integers in the range [1,60), i.e., at least 1,
  118. # but less than 60. In reality, these five numbers must all be distinct, but for this problem,
  119. # we will allow duplicates. The Powerball number is a random integer in the range [1,36),
  120. # i.e., at least 1 but less than 36. Use the random module and the function random.randrange to
  121. # generate the appropriate random numbers.Note that this function should print the desired message,
  122. # rather than returning it as a string.
  123. import random
  124. def powerball():
  125. ball1, ball2, ball3, ball4, ball5 = random.sample(range(1,60), 5)
  126. ball6 = random.choice(range(1, 36))
  127. message = "Today's numbers are %d, %d, %d, %d, and %d. The Powerball number is %d." % (ball1, ball2, ball3, ball4, ball5, ball6)
  128. print(message)
  129. powerball()
  130. powerball()
  131. print('=====')

An Introduction to Interactive Programming in Python (Part 1) -- Week 2_1 练习的更多相关文章

  1. An Introduction to Interactive Programming in Python (Part 1) -- Week 2_3 练习

    Mini-project description - Rock-paper-scissors-lizard-Spock Rock-paper-scissors is a hand game that ...

  2. An Introduction to Interactive Programming in Python

    这是在coursera上面的一门学习pyhton的基础课程,由RICE的四位老师主讲.生动有趣,一共是9周的课程,每一周都会有一个小游戏,经历一遍,对编程会产生很大的兴趣. 所有的程序全部在老师开发的 ...

  3. Mini-project # 1 - Rock-paper-scissors-___An Introduction to Interactive Programming in Python"RICE"

    Mini-project description - Rock-paper-scissors-lizard-Spock Rock-paper-scissors is a hand game that ...

  4. An Introduction to Interactive Programming in Python (Part 1) -- Week 2_2 练习

    #Practice Exercises for Logic and Conditionals # Solve each of the practice exercises below. # 1.Wri ...

  5. 【python】An Introduction to Interactive Programming in Python(week two)

    This is a note for https://class.coursera.org/interactivepython-005 In week two, I have learned: 1.e ...

  6. Quiz 6b Question 8————An Introduction to Interactive Programming in Python

     Question 8 We can use loops to simulate natural processes over time. Write a program that calcula ...

  7. Quiz 6b Question 7————An Introduction to Interactive Programming in Python

     Question 7 Convert the following English description into code. Initialize n to be 1000. Initiali ...

  8. Quiz 6a Question 7————An Introduction to Interactive Programming in Python

     First, complete the following class definition: class BankAccount: def __init__(self, initial_bal ...

  9. Mini-project # 4 - "Pong"___An Introduction to Interactive Programming in Python"RICE"

    Mini-project #4 - "Pong" In this project, we will build a version of Pong, one of the firs ...

随机推荐

  1. iOS之 开发中用得到的开源github

    github:无限图片轮播 https://github.com/dymx101/DYMRollingBanner 2.灌水动画 https://github.com/dsxNiubility/SXW ...

  2. Oracle查看所有用户

    1.查看所有用户:select * from dba_users;   select * from all_users;   select * from user_users; 2.查看用户或角色系统 ...

  3. 获取微信openID 的步骤

    获取微信openid的步骤:1.进入-->判断openID是否为空: 空-->$url=urlencode("http://xxx/xxx.php");//回调链接 $ ...

  4. javascript/jquery 常见功能实现(持续更新...)

    1. input 只能输入整数数字和字母 $(document).on('keyup','#no',function(){ var val = $.trim($(this).val()); if(va ...

  5. SQL Server 2008 R2——VC++ ADO 操作 存储过程

    ==================================声明================================== 本文原创,转载在正文中显要的注明作者和出处,并保证文章的完 ...

  6. 用PowerDesigner将SQL语句生成实体类

    1.首先打开PowerDesigner,点击左上角“File”—>"Reverse Engineer"—>"Database..." 2.选择数据库 ...

  7. Redis系列(三)—— 订阅/发布

    Redis 订阅/发布 参考:http://www.cnblogs.com/mushroom/p/4470006.html,http://www.tuicool.com/articles/ABry2a ...

  8. MCS51系列单片机实用技术部分课件

  9. python strip() lstrip() rstrip() 使用方法

    Python中的strip用于去除字符串的首尾字符串,同理,lstrip用于去除最左边的字符,rstrip用于去除最右边的字符. 这三个函数都可传入一个参数,指定要去除的首尾字符. 需要注意的是,传入 ...

  10. 点击页面div弹窗以外隐藏的两种思路

    在本文为大家介绍两种思路实现点击页面其它地方隐藏该div,第一种是对document的click事件绑定事件处理程序.. 第一种思路分两步 第一步:对document的click事件绑定事件处理程序, ...