案例一,有关于数据类型
num1 = 10 num2 = 10.00 string1 ='10.00' string2 ="10.00" string3 ='''10.00''' print(type(num1)) print(type(num2)) print(type(string1)) print(type(string2)) print(type(string3))
案例二,披萨点单系统
number = int(input("请问您需要多少份披萨?"))
#input函数接受键盘输入的披萨份数,再用int对键入的数据进行强制转型成int型
per_pizza = float(input("一份pizza多少元?"))
#input函数接受键盘输入的披萨份数,再用int对键入的数据进行强制转型成float型
total = number*per_pizza
#上面已经转成可参与运算的类型,可以进行乘法运算
print("您共需要支付"+str(total)+'元')
#输出用“+"拼接字符串
python的turtle项目:送你一朵小红花
import turtle#导入海龟画图库
t = turtle.Pen()
t.pensize(10)
t.speed(0)#导入海龟画图后,设置好画笔的大小,速度
str_num = 5
int_num = 3
float_num = 1
colors = ['purple','orange','green','yellow','blue','red','white','gray']#创建一个列表存放颜色元素
for x in range(str_num):#用定义出来的str_num来进行循环控制
t.pencolor(colors[str_num])#从颜色列表中选取具体颜色,索引号是循环便利序号,第5个,红色
t.circle(360/str_num)
t.left(360/str_num + 1)
#至此,画出5个红色的相交的圆
for x in range(int_num):
t.pencolor(colors[int_num])
t.circle(30)
t.left(360/int_num + 1)
#至此,画出3个黄色的相交的圆
t.goto(0,-10)
for x in range(float_num):
t.pencolor(colors[float_num])
t.circle(10)
t.left(360/float_num + 1)
#至此,画出1个橙色的相交的圆
turtle.done()
数据类型转换案例(BMI值计算):
height = float(input('请输入您的身高(m):'))
weight = float(input('请输入您的体重(kg):'))
BMI = weight/(height**2)
print('您的BMI值为:'+str(BMI))
print('您的BMI值为(取整):'+str(int(BMI)))
小游戏:更改前面的输入代码就好
# SmileyPong.py
##########################################################
##########################################################
lives=int(input("请输入你的生命值:"))
add=float(input("每接到一次球将增加的得分为:"))
##########################################################
##########################################################
import pygame
pygame.init()
screen = pygame.display.set_mode([800,600])
pygame.display.set_caption("Smiley Pong")
keepGoing = True
pic = pygame.image.load("CrazySmile.bmp")
colorkey = pic.get_at((0,0))
pic.set_colorkey(colorkey)
picx = 0
picy = 0
BLACK = (0,0,0)
WHITE = (255,255,255)
timer = pygame.time.Clock()
speedx = 5
speedy = 5
paddlew = 200
paddleh = 25
paddlex = 300
paddley = 550
picw = 100
pich = 100
points = 0
font = pygame.font.SysFont("Times", 24)
while keepGoing: # Game loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_F1: # F1 = New Game
points = 0
##########################################################
##########################################################
lives=int(input("请输入你的生命值:"))
add=float(input("每接到一次球将增加的得分为:"))
##########################################################
##########################################################
picx = 0
picy = 0
speedx = 5
speedy = 5
picx += speedx
picy += speedy
if picx <= 0 or picx >= 700:
speedx = -speedx * 1.1
if picy <= 0:
speedy = -speedy + 1
if picy >= 500:
lives -= 1
speedy = -5
speedx = 5
picy = 499
screen.fill(BLACK)
screen.blit(pic, (picx, picy))
# Draw paddle
paddlex = pygame.mouse.get_pos()[0]
paddlex -= paddlew/2
pygame.draw.rect(screen, WHITE, (paddlex, paddley, paddlew, paddleh))
# Check for paddle bounce
if picy + pich >= paddley and picy + pich <= paddley + paddleh
and speedy > 0:
if picx + picw/2 >= paddlex and picx + picw/2 <= paddlex +
paddlew:
speedy = -speedy
points += add
# Draw text on screen
draw_string = "Lives: " + str(lives) + " Points: " + str(points)
# Check whether the game is over
if lives < 1:
speedx = speedy = 0
draw_string = "Game Over. Your score was: " + str(points)
draw_string += ". Press F1 to play again. "
text = font.render(draw_string, True, WHITE)
text_rect = text.get_rect()
text_rect.centerx = screen.get_rect().centerx
text_rect.y = 10
screen.blit(text, text_rect)
pygame.display.update()
timer.tick(60)
pygame.quit() # Exit



