#GuessingGame.py
import random
the_number = random.randint(1, 10)
print("计算机已经在1到10之间随机生成了一个数字,")
guess = int(input("请你猜猜是哪一个数字: "))
while guess != the_number:
if guess > the_number:
print(guess, "猜大了!")
if guess < the_number:
print(guess, "猜小了!")
guess = int(input("再猜一次: "))
print(guess, "猜对了! ")
修改上面的程序让计算机生成1到100之间的随机数字。
继续修改,创建一个变量number_of_tries来记录用户猜测的次数,每猜一次,就将该变量加1,猜对后,显示用户猜测的次数。
添加一个外部循环来询问用户,当猜对后是否还想再玩一次。
颜色列表=["red","green","yellow"]
random.choice(颜色列表)
笛卡尔坐标系,即x,y坐标系,屏幕中心为原点(0,0)
设置海龟的位置:turtle.setpos(x,y)
turtle.penup(),抬起钢笔,要画线时再pendown()
turtle.window_width()获取窗口宽度
turtle.window_height()获取窗口高度
计算随机坐标:x=random.randrange(-turtle.window_width()//2,turtle.window_width()//2)
y=random.randrange(-turtle.window_height()//2,turtle.window_height()//2)
#RandomSpirals.py
import random
import turtle as t
t.bgcolor("black")
colors = ["red", "yellow", "blue", "green", "orange", "purple",
"white", "gray"]
for n in range(50):
# Generate spirals of random sizes/colors at random locations
t.pencolor(random.choice(colors)) # Pick a random color
size = random.randint(10,40) # Pick a random spiral size
# Generate a random (x,y) location on the screen
x = random.randrange(-t.window_width()//2,
t.window_width()//2)
y = random.randrange(-t.window_height()//2,
t.window_height()//2)
t.penup()
t.setpos(x,y)
t.pendown()
for m in range(size):
t.forward(m*2)
t.left(91)