Python 课堂记录

猜数字

import turtle
import random
import time #时间模块
#生成随机数
ran=random.randint(1,100) # int :整数
c=0
#无限重复执行
while True:
c+=1 # 自增一 c=c+1
# 等一秒
time.sleep(1)
turtle.clear()
turtle.write(“第”+str©+“次:”,font=(“黑体”,16) )
#等待询问
an=turtle.numinput(“提示”,“输入一个数字”)
#清屏
turtle.clear()
#比较 1、大了 2、小了 3、相等
if an==ran:
turtle.write(“猜对了”,font=(“黑体”,16) )
#退出循环
break
elif an>ran:
turtle.write(“猜大了”,font=(“黑体”,16) )
else: #其它
turtle.write(“猜小了”,font=(“黑体”,16) )
turtle.done()

模拟硬币

import random
import time #时间模块
c=0
c1=0
for i in range(100000):
#生成随机数
ran=random.randint(0,1) # int :整数 0 反面 1 正面
if ran==0:
c+=1
else:
c1+=1

print(c,“\t”,c1)

#类型转换
a=1 # str(1)->“1”
b=int(“1”) # int(“1”)–>1 int 表示整数
c=a+b # + 如果是字符串,表示连接 如果是数字,加法操作
print©

import random
import turtle #模块:仓库,很多的工具(函数),提供一定的功能
#随机模块
#1、 绘制一个气球 2、画布选择一个随机位置,重复绘制气球
#线(fd)+点(dot) seth()
turtle.speed(100)

满天的气球

#0 1 2 3 4 5
cs=[“red”,“orange”,“green”,“yellow”,“pink”,“blue”]

for i in range(40):

x = random.randint(-300, 300) #(x,y)
y = random.randint(-400, 250)
turtle.up()
turtle.goto(x,y)
turtle.down()

turtle.seth(90)
s = random.randint(30, 60)
turtle.fd(s+20)
turtle.dot(s,  cs[ i%6 ] )

turtle.hideturtle()
turtle.done()

import random
import turtle

turtle.speed(100)
turtle.bgcolor(“black”)

星射线

#0 1 2 3 4 5
cs = [“red”, “orange”, “green”, “yellow”, “pink”, “blue”]

for i in range(100):
x = random.randint(0, 360) # (x,y)
turtle.seth(x)
s = random.randint(80, 250)
turtle.color(cs[i%5] )
turtle.fd(s)
turtle.fd(-s)

turtle.hideturtle()
turtle.done()

百钱百鸡

#1只公鸡5元, 1只母鸡3元, 3只小鸡1元, 用100元买100只鸡,请问多少方案?
#公鸡:1~20
#母鸡:1~33
for gj in range(1,21):
for mj in range(1,34):
xj=100-gj-mj
if xj%30 and gj5+mj3+xj//3100:
print(gj,mj,xj)

倒置九九乘法表

for j in range(9,0,-1): # j:9,8,7,6,5,4,3,2,1 行号
#一行
for i in range(1,j+1): #i 列号
s=str(i)+“x”+str(j)+“=”+str(i*j)
print(s,end=“\t”) # 默认换行 \ 转义符 \t 空格

print()

k=1
s=4
for i in range(5):

for l in range(s):
    print("\t",end="")
s=s-1 # 4 3 2 1 0

for j in range(k):
    print("*",end="\t")
print()
k=k+2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一枚大果壳

码文不易,晚上熬夜需要点咖啡钱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值