python基础语法小案例

案例1:

1、输入三个整数x,y,z,请把这三个数由大到小输出

x=int(input("请输入整数X:"))
y=int(input("请输入整数y:"))
z=int(input("请输入整数z:"))
if x>y>z:
    print(f'{x}>{y}>{z}')
elif x>z>y:
    print(f'{x}>{z}>{y}')
elif y>x>z:
    print(f'{y}>{x}>{z}')
elif y>z>x:
    print(f'{y}>{z}>{x}')
elif z>y>x:
    print(f'{z}>{y}>{x}')
else:
    print(f'{z}>{x}>{y}')


在这里插入图片描述

案例2:

2、猜拳游戏:

用户输入石头、剪刀或布,电脑也会出一个招,要求得出
最终结果,显示胜利玩家。
提示1:用数字代表猜拳
提示2:使用条件判断语句
提示3:
import random
random.randint(m,n )为取m-n随机整数的方法

import random
# 1. 出拳
# 玩家
player = int(input('请出拳:0--石头;1--剪刀;2--布:'))
# 电脑
# computer = 1
computer = random.randint(0, 2)
# print(computer)

# 2. 判断输赢
# 玩家获胜
if ((player == 0) and (computer == 1)) or ((player == 1) and (computer == 2)) or ((player == 2) and (computer == 0)):
    print('玩家获胜,哈哈哈哈')
# 平局
elif player == computer:
    print('平局,别走,再来一局')
else:
    print('电脑获胜')

在这里插入图片描述

案例3:

3、提示输入1-3之间的数据,若输入1则画一个三角形(等 边三角形,边长90);若输入2则画一个圆形(半径90); 若输入3则画一个正方形(边长90)。

import turtle
num=int(input("请输入1-3:"))
if num==1:
    print(f"您输入的是{num},将输出三角形")
    for i in range(3):
        turtle.seth(i * 120)
        turtle.fd(90)
    turtle.exitonclick()
elif num==2:
    print(f'您输入的是{num},将输出圆形')
    turtle.circle(90,360)
    turtle.exitonclick()
elif num==3:
    print(f'您输入的是{num},将输出正方形')
    for i in range(4):
       turtle.fd(90)
       turtle.left(90)
    turtle.exitonclick()
else:
    print(input('你个沙雕,输1-3的数'))

在这里插入图片描述
在这里插入图片描述

案例4:

4.需求:依次输入三角形的三边长,判断能否生成一个三角形(任 意两边之和大于第三边)。若能组成三角形判断是否能组成等边 三角形;若能则画出等边三角形。

import turtle
a=int(input("请输入a边长:"))
b=int(input('请输入b边长:'))
c=int(input("请输入C边长:"))
if a > 0 and b > 0 and c > 0:
    if a + b > c and b + c > a and a + c > b:
        if a == b and b == c:
            print("这是等边三角形")
            for i in range(3):
                turtle.seth(i * 120)
                turtle.fd(100)
            turtle.exitonclick()
        elif a == b or b == c or c == a:
            print ("这是等腰三角形")
        else:
            print ("这是不规则三角形")
    elif a + b == c or b + c == a or a + c == b:
        print ("这是个直角三角形")
    else:
        print ('这好像不是个三角形')
else:
    print("请输入大于0的数字")



在这里插入图片描述
在这里插入图片描述

案例5:

5.画一个小汽车

import turtle
Bob=turtle.Turtle()
Bob.shape('turtle')
Bob.color('red')
Bob.begin_fill()
Bob.forward(200)
Bob.right(90)
Bob.forward(100)
Bob.left(90)
Bob.forward(200)
Bob.right(90)
Bob.forward(100)
Bob.right(90)
Bob.forward(400)
Bob.right(90)
Bob.forward(200)
Bob.end_fill()
Bob.right(180)
Bob.forward(200)
Bob.penup()
Bob.forward(100)
Bob.left(90)
Bob.forward(100)
Bob.pendown()
Bob.color('black')
Bob.begin_fill()
Bob.circle(60)
Bob.end_fill()
Bob.penup()
Bob.forward(200)
Bob.pendown()
Bob.begin_fill()
Bob.circle(60)
Bob.end_fill()
turtle.exitonclick()

在这里插入图片描述

原文地址:https://www.cnblogs.com/James-221/p/13647431.html