Shandong University "Python programming and big data analytics" lab2

Experiment two: Python Programming Language Examples Analysis and Verification

First, the type of experiment: Prototype Design Type +
Second, the suggestion hours: 4
Third, the purpose of the experiment:

1, continues familiar IDLE, the basic operation Python development environment familiar Python usage basic input and output functions or statements familiar to import and use Python standard libraries and extension libraries;
2, control using program control structures programming method;
3, master Python built-in random function usage

Fourth, the Experiment:

(1) Prototype: BMI Textbook example program in Chapter 4 and selected from the PI calculation procedure of Example 4.3 random authentication table common function (not submitted).
(2) Design Type: teaching material "python language programming foundation (2nd Edition)" in Chapter 4 Exercises Option:
4.1,4.2,4.3,4.4,4.5,4.7.

Fifth, the experimental teaching materials: "Python language programming foundation (2nd Edition)"
  • "Temperature Conversion Example" Code Example:
    1.1.
#e1.1TempConvert.py
TempStr=input("请输入带有符号的温度值:")
if TempStr[-1] in ['F','f']:
    C=(eval(TempStr[0:-1])-32)/1.8
    print("转换后的温度是{:.2f}C".format(C))
elif TempStr[-1] in ['C','c']:
    F=1.8*eval(TempStr[0:-1])+32;
    print("转换后的温度是{:.2f}F".format(F))
else:
    print("输入格式错误")

Output:Here Insert Picture Description

1.2.

#e1.2TempConvert.py
TempStr=input("请输入带有符号的温度值:")
while TempStr[-1] not in['N','n']:
    if TempStr[-1] in ['F','f']:
        C=(eval(TempStr[0:-1])-32)/1.8
        print("转换后的温度是{:.2f}C".format(C))
    elif TempStr[-1] in ['C','c']:
        F=1.8*eval(TempStr[0:-1])+32;
        print("转换后的温度是{:.2f}F".format(F))
    else:
        print("输入格式错误")
TempStr=input("请输入带有符号的温度值:")

Output:Here Insert Picture Description

1.3.

#e1.3TempConvert.py
def tempConvert(ValueStr):
    if TempStr[-1] in ['F','f']:
        C=(eval(TempStr[0:-1])-32)/1.8
        print("转换后的温度是{:.2f}C".format(C))
    elif TempStr[-1] in ['C','c']:
        F=1.8*eval(TempStr[0:-1])+32;
        print("转换后的温度是{:.2f}F".format(F))
    else:
        print("输入格式错误")
TempStr=input("请输入带有符号的温度值:")
tempConvert(TempStr)

Output:Here Insert Picture Description

  • "Draw the Python python Examples of" Code Example
    2.1.
#e2.1DrawPython.py
import turtle
turtle.setup(650,350,200,200)
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(25)
turtle.pencolor("purple")
turtle.seth(-40)
for i in range(4):
    turtle.circle(40,80)
    turtle.circle(-40,80)
turtle.circle(40,80/2)
turtle.fd(40)
turtle.circle(16,180)
turtle.fd(40*2/3) 

2.2.

#e2.2DrawPython.py
from turtle import *
setup(650,350,200,200)
penup()
fd(-250)
pendown()
pensize(25)
pencolor("purple")
seth(-40)
for i in range(4):
    circle(40,80)
    circle(-40,80)
circle(40,80/2)
fd(40)
circle(16,180)
fd(40*2/3)

2.3.

#e2.3DrawPython.py
import turtle
def drawSnake(radius,angle,length):
    turtle.seth(-40)
    for i in range(length):
        turtle.circle(radius,angle)
        turtle.circle(-radius,angle)
    turtle.circle(radius,angle/2)
    turtle.fd(40)
    turtle.circle(16,180)
    turtle.fd(40*2/3)
turtle.setup(650,350,200,200)
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(25)
turtle.pencolor("purple")
drawSnake(40,80,4)
turtle.done()

Here Insert Picture Description

  • Chapter 2 program Exercises:
    2.1.
TempStr=eval(input("转摄氏温度请输1,转华氏温度请输2:"))
if TempStr==1:
    F=eval(input("请输入华氏温度:"))
    C=(F-32)/1.8
    print("转换后的温度是{:.0f}C".format(C))
elif TempStr==2:
    C=eval(input("请输入摄氏温度:"))
    F=1.8*C+32;
    print("转换后的温度是{:.0f}F".format(F))
else:
    print("输入格式错误")

Output:Here Insert Picture Description

2.2.

TempStr=input("请输入带有符号($或¥)的金额:")
if TempStr[0] in ['¥']:
    D=eval(TempStr[1:])*6
    print("转换后的金额是${:.2f}".format(D))
elif TempStr[0] in ['$']:
    R=eval(TempStr[1:])/6;
    print("转换后的金额是¥{:.2f}F".format(R))
else:
    print("输入格式错误")

Output:Here Insert Picture Description

2.3.

import turtle
turtle.setup(650,350,200,200)
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(25)
turtle.seth(-40)
colors=["red","orange","yellow","green"]
for i in range(4):
    turtle.pencolor(colors[i])
    turtle.circle(40,80)
    turtle.circle(-40,80)
turtle.pencolor("cyan")
turtle.circle(40,80/2)
turtle.fd(40)
turtle.pencolor("blue")
turtle.circle(16,180)
turtle.fd(40*2/3) 

Here Insert Picture Description
2.5.

import turtle
angle=60
turtle.seth(angle)
turtle.fd(150)
for i in range(6):
    if(i==3):
        angle-=60
    else:
        angle-=120
    turtle.seth(angle)
    turtle.fd(150)
    if i<2:
        turtle.fd(150)

Here Insert Picture Description
2.7.

import turtle
angle=0
turtle.seth(angle)
for i in range(4):
    turtle.fd(150)
    turtle.penup()
    turtle.fd(50)
    angle+=90
    turtle.seth(angle)
    turtle.fd(50)
    turtle.pendown()

Here Insert Picture Description
2.8.

import turtle
angle=90
turtle.seth(angle)
turtle.fd(60)
for i in range(7):
    if(i==3):
        angle-=60
    else:
        angle-=120
    turtle.seth(angle)
    turtle.fd(120)
    if i!=2 and i!=3:
        turtle.fd(60)

Here Insert Picture Description

Published 13 original articles · won praise 0 · Views 84

Guess you like

Origin blog.csdn.net/weixin_43959421/article/details/103978001