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

简介:
# Practice Exercises for Functions # Solve each of the practice exercises below. # 1.Write a Python function miles_to_feet that takes a parameter miles and # returns the number of feet in miles miles. def miles_to_feet(miles): feet = miles * 5280 return feet print (miles_to_feet( 2.5 )) print ( '=====' ) # 2.Write a Python function total_seconds that takes three parameters hours, minutes and seconds and # returns the total number of seconds for hours hours, minutes minutes and seconds seconds. def total_seconds(hours, minutes, seconds): total = hours * 60 * 60 + minutes * 60 + seconds return total print (total_seconds( 1 , 5 , 10 )) print ( '=====' ) # 3.Write a Python function rectangle_perimeter that takes two parameters width and height # corresponding to the lengths of the sides of a rectangle and # returns the perimeter of the rectangle in inches. def rectangle_perimeter(width, height): perimeter = (width + height) * 2 return perimeter print (rectangle_perimeter( 2.3 , 2.2 )) print ( '=====' ) # 4.Write a Python function rectangle_area that takes two parameters width and height # corresponding to the lengths of the sides of a rectangle and # returns the area of the rectangle in square inches. def rectangle_area(width, height): area = width * height return area print (rectangle_area( 2 , 5 )) print ( '=====' ) # 5.Write a Python function circle_circumference that takes a single parameter radius # corresponding to the radius of a circle in inches and # returns the the circumference of a circle with radius radius in inches. # Do not use π=3.14, instead use the math module to supply a higher-precision approximation to π. import math def circle_circumference(radius): circumference = 2.0 * radius * math.pi return circumference print (circle_circumference( 4.0 )) print ( '=====' ) # 6.Write a Python function circle_area that takes a single parameter radius # corresponding to the radius of a circle in inches and # returns the the area of a circle with radius radius in square inches. # Do not use π=3.14, instead use the math module to supply a higher-precision approximation to π. def circle_area(radius): area = radius * radius * math.pi return area print (circle_area( 4.0 )) print ( '=====' ) # 7.Write a Python function future_value that takes three parameters present_value, annual_rate and years and # returns the future value of present_value dollars invested at annual_rate percent interest, # compounded annually for years years. def future_value(present_value, annual_rate, years): value = present_value * pow (annual_rate + 1.0 , years) return value print (future_value( 1000000.0 , 0.03 , 10 )) print ( '=====' ) # 8.Write a Python function name_tag that takes as input the parameters first_name and last_name (strings) and # returns a string of the form "My name is % %." where the percents are the strings first_name and last_name. # Reference the test cases in the provided template for an exact description of # the format of the returned string. def name_tag(first_name, last_name): form = "My name is %s %s ." % (first_name, last_name) return form print (name_tag( 'Bob' , 'Smith' )) print ( '=====' ) # 9.Write a Python function name_and_age that takes as input the parameters name (a string) and age (a number) and # returns a string of the form "% is % years old." where the percents are the string forms of name and age. # Reference the test cases in the provided template for an exact description of # the format of the returned string. def name_and_age(name, age): form = " %s is %d years old." % (name, age) return form print (name_and_age( 'John' , 24 )) print ( '=====' ) # 10.Write a Python function point_distance that takes as the parameters x0, y0, x1 and y1, and # returns the distance between the points (x0,y0) and (x1,y1). def point_distance(x0, y0, x1, y1): distance = math.sqrt((x0 - x1) ** 2 + (y0 - y1) ** 2 ) return distance print (point_distance( 0 , 0.5 , - 2.2 , 3.5 )) print ( '=====' ) # 11.Challenge: Write a Python function triangle_area that takes the parameters x0, y0, x1,y1, x2, and y2, and # returns the area of the triangle with vertices (x0,y0), (x1,y1) and (x2,y2). # (Hint: use the function point_distance as a helper function and apply Heron's formula.) def triangle_area(x0, y0, x1, y1, x2, y2): side1 = point_distance(x0, y0, x1, y1) side2 = point_distance(x1, y1, x2, y2) side3 = point_distance(x2, y2, x0, y0) area = heron_formula(side1, side2, side3) return area # 海伦公式 def heron_formula(side1, side2, side3): p = (side1 + side2 + side3) / 2.0 area = math.sqrt(p * (p - side1) * (p - side2) * (p - side3)) return area print (triangle_area( 0 , 0.5 , - 2.2 , 3.5 , - 3 , - 2.5 )) print ( '=====' ) # 12.Challenge: Write a Python function print_digits that takes an integer number in the range [0,100), # i.e., at least 0, but less than 100. It prints the message "The tens digit is %, and the ones digit is %.", # where the percent signs should be replaced with the appropriate values. # (Hint: Use the arithmetic operators for integer division // and remainder % to find the two digits. # Note that this function should print the desired message, rather than returning it as a string. def print_digits(number): tens, ones = number // 10 , number % 10 message = "The tens digit is %d , and the ones digit is %d ." % (tens, ones) print (message) print_digits( 49 ) print ( '=====' ) # 13.Challenge: Powerball is lottery game in which 6 numbers are drawn at random. # Players can purchase a lottery ticket with a specific number combination and, # if the number on the ticket matches the numbers generated in a random drawing, # the player wins a massive jackpot. Write a Python function powerball that takes no arguments and # prints the message "Today's numbers are %, %, %, %, and %. The Powerball number is %.". # The first five numbers should be random integers in the range [1,60), i.e., at least 1, # but less than 60. In reality, these five numbers must all be distinct, but for this problem, # we will allow duplicates. The Powerball number is a random integer in the range [1,36), # i.e., at least 1 but less than 36. Use the random module and the function random.randrange to # generate the appropriate random numbers.Note that this function should print the desired message, # rather than returning it as a string. import random def powerball(): ball1, ball2, ball3, ball4, ball5 = random.sample( range ( 1 , 60 ), 5 ) ball6 = random.choice( range ( 1 , 36 )) message = "Today's numbers are %d , %d , %d , %d , and %d . The Powerball number is %d ." % (ball1, ball2, ball3, ball4, ball5, ball6) print (message) powerball() powerball()

print('=====')


本文转自罗兵博客园博客,原文链接:http://www.cnblogs.com/hhh5460/p/5774925.html,如需转载请自行联系原作者

相关文章
|
SQL 算法 数据挖掘
动态规划Dynamic programming详解-编辑距离问题【python】
动态规划Dynamic programming详解-编辑距离问题【python】
|
存储 SQL 算法
动态规划Dynamic programming详解-背包问题【python】
动态规划Dynamic programming详解-背包问题【python】
|
存储 SQL 算法
动态规划Dynamic programming详解-硬币找零问题【python】
动态规划Dynamic programming详解-硬币找零问题【python】
|
存储 算法 数据挖掘
动态规划Dynamic programming详解-最长公共子序列【python】
动态规划Dynamic programming详解-最长公共子序列【python】
|
算法 决策智能 Python
Python高级算法——线性规划(Linear Programming)
Python高级算法——线性规划(Linear Programming)
1147 0
Python高级算法——线性规划(Linear Programming)
|
算法 Python
Python算法之动态规划(Dynamic Programming)解析:二维矩阵中的醉汉(魔改版leetcode出界的路径数)
现在很多互联网企业学聪明了,知道应聘者有目的性的刷Leetcode原题,用来应付算法题面试,所以开始对这些题进行“魔改”,比如北京某电商平台的这道题: 有一个正方形的岛,使用二维方形矩阵表示,岛上有一个醉汉,每一步可以往上下左右四个方向之一移动一格,如果超出矩阵范围他就死了,假设每一步的方向都是随机的(因为他是醉的),请计算n步以后他还活着的概率。
Python算法之动态规划(Dynamic Programming)解析:二维矩阵中的醉汉(魔改版leetcode出界的路径数)
|
安全 物联网 测试技术
书籍:python物联网编程项目 Internet of Things Programming Projects - 2018
简介 利用Raspberry Pi 3和Python的组合,充分发挥物联网的潜力 使用IoT构建复杂的基于Python的应用程序 从事各种物联网项目,了解电子产品的基础知识 物联网(IOT)已成功吸引了研究人员和技术爱好者的注意力,因为它将经典网络与仪器和设备完美结合。
|
测试技术 API SDN
书籍:python网络编程 Python Network Programming - 2019
简介 主要特点 掌握Python技能,开发强大的网络应用程序 掌握SDN的基本原理和功能 为echo和chat服务器设计多线程,事件驱动的体系结构 此学习路径强调了Python网络编程的主要方面,例如编写简单的网络客户端,创建和部署SDN和NFV系统,以及使用Mininet扩展您的网络。
|
传感器 人工智能 机器人
python人工智能机器人工具书籍: Learn Robotics Programming(python and Raspberry Pi 3) - 2018
简介 人工智能和智能机器人将精确有效地执行不同的任务。 Raspberry Pi和Python的组合在制作这些机器人时非常有效。 本书首先向您介绍机器人的基本结构,以及如何规划,构建和编程。 当您完成本书时,您将逐渐进步添加不同的输出和传感器,学习新的建筑技能,以及使用传感器编写有趣行为的代码。

推荐镜像

更多