43张正杰

导航

Python输入输出练习,运算练习,turtle初步练习

题目:

Hello World!
简单交互(交互式,文件式)教材P19
用户输入两个数字,计算并输出两个数字之和(尝试用一行代码实现)。
用户输入三角形三边长度,并计算三角形的面积:(海伦公式)
输入半径,计算圆的面积。
画一组同切圆
画一个五角星
画一个全黄色的五角星

1、Hello world!

>>> print('Hello world!')
Hello world!

 

2、简单交互

name = input("输入姓名:")
print("{}同学,学好python,前途无量!".format(name))
print("{}大侠,学好python,前途无量!".format(name[0]))
print("{}哥哥,学好python,前途无量!".format(name[1:]))

      结果:

 

 

3、简单计算

n1 = input('请输入第一个数字:')
n2 = input('请输入第二个数字:')
sum = int(n1)+int(n2)
print("sum=%d"%(sum))

     结果:

     使用一行代码:

print('两个数的和是%.2f'%(float(input('请输入第一个数字:'))+float(input('请输入第二个数字:'))))

     结果:

 

4、计算三角形的面积

l1 = float(input('请输入第一条边的长度:'))
l2 = float(input('请输入第二条边的长度:'))
l3 = float(input('请输入第三条边的长度:'))
p = (l1+l2+l3) / 2
s = (p*(p-l1)*(p-l2)*(p-l3))**0.5
print('三角形的面积为:%.2f'%s)

     结果:

 

5、计算圆的面积

r = float(input('请输入圆的半径:'))
s = r**2*3.14
print('圆的面积为:%.2f'%s)

     结果:

 

 

6、画一组同切圆

import turtle
turtle.circle(50)
turtle.circle(100)
turtle.circle(150)
turtle.circle(200)

     结果:

 

7、画一个五角星

import turtle
turtle.forward(200)
turtle.right(144)
turtle.forward(200)
turtle.right(144)
turtle.forward(200)
turtle.right(144)
turtle.forward(200)
turtle.right(144)
turtle.forward(200)

     结果:

 

8、画一个全黄色的五角星

import turtle
turtle.color('yellow')
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.forward(200)
turtle.right(144)
turtle.forward(200)
turtle.right(144)
turtle.forward(200)
turtle.right(144)
turtle.forward(200)
turtle.right(144)
turtle.forward(200)
turtle.end_fill()

     结果:

 

posted on 2017-09-07 19:56  43张正杰  阅读(802)  评论(0编辑  收藏  举报