找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 1256|回复: 9

[代码与实例] 猜单词

1

主题

10

帖子

10

积分

贫民

积分
10
ych 发表于 2022-8-1 21:41:34 | 显示全部楼层 |阅读模式
import random
lives=9
words=['pizza','fairy','teeth','shirt','otter','plane']
secret_word=random.choice(words)
clue=list('?????')
heart_symbol=u'\u2764'
guessed_word_correctly=False
unknown_letters=len(secret_word)
def update_clue(guessed_letter,secret_word,clue,unknown_letters):
    index=0
    while index<len(secret_word):
        if guessed_letter==secret_word[index]:
            clue[index]=guessed_letter
            unknown_letters=unknown_letters-1
        index=index+1
    return unknown_letters
while lives>0:
    print(clue)
    print("Lives left"+heart_symbol*lives)
    guess=input('Guess a letter or the whole word:')
    if guess==secret_word:
        guessed_word_correctly=True
        break
    if guess in secret_word:
        unknown_letters=update_clue(guess,secret_word,clue,unknown_letters)
    else:
        print('Incorrect.You lose a life')
        lives=lives-1
    if unknown_letters==0:
        guessed_word_correctly=True
        break
if guessed_word_correctly:
    print('You won!The secret word was '+secret_word)
else:
    print('You lost! The secret word was '+secret_word)







回复

使用道具 举报

1

主题

10

帖子

10

积分

贫民

积分
10
ych  楼主| 发表于 2022-8-1 21:45:10 | 显示全部楼层
import turtle as t
from itertools import cycle
color=cycle(['red','orange','yellow','green','blue','purple'])
def draw_circle(size):
    t.pencolor(next(color))
    t.circle(size)
    draw_circle(size+5)
t.bgcolor('black')
t.speed('fast')
t.pensize(4)
draw_circle(30)



回复 支持 反对

使用道具 举报

1

主题

10

帖子

10

积分

贫民

积分
10
ych  楼主| 发表于 2022-8-1 21:49:21 | 显示全部楼层
import turtle as t
from itertools import cycle
color=cycle(['red','orange','yellow','green','blue','purple'])
def draw_circle(size,angle,shift):
    t.bgcolor(next(color))
    t.pencolor(next(color))
    t.circle(size)
    t.right(angle)
    t.forward(shift)
    draw_circle(size+5,angle+1,shift+1)
t.speed('fast')
t.pensize(4)
draw_circle(30,0,1)
   
回复 支持 反对

使用道具 举报

1

主题

10

帖子

10

积分

贫民

积分
10
ych  楼主| 发表于 2022-8-1 21:52:50 | 显示全部楼层
import turtle as t
def rectangle(horizontal,verticle,color):
    t.pendown()
    t.pensize(1)
    t.color(color)
    t.begin_fill()
    for counter in range(1,3):
        t.forward(horizontal)
        t.right(90)
        t.forward(verticle)
        t.right(90)
    t.end_fill()
    t.penup()
    t.speed('slow')
    t.bgcolor('Dodger blue')
#feet
t.penup()
t.goto(-100,-150)
rectangle(50,20,'blue')
t.goto(-30,-150)
rectangle(50,20,'blue')
#legs
t.goto(-25,-50)
rectangle(15,100,'grey')
t.goto(-55,-50)
rectangle(-15,100,'grey')
#body
t.goto(-90,100)
rectangle(100,150,'red')
#arms
t.goto(-150,70)
rectangle(60,15,'grey')
t.goto(-150,110)
rectangle(15,40,'grey')
t.goto(10,70)
rectangle(60,15,'grey')
t.goto(55,110)
rectangle(15,40,'grey')
#neck
t.goto(-50,120)
rectangle(15,20,'grey')
#head
t.goto(-85,170)
rectangle(80,50,'red')
#eyes
t.goto(-60,160)
rectangle(30,10,'white')
t.goto(-55,155)
rectangle(5,5,'black')
t.goto(-40,155)
rectangle(5,5,'black')
#mouth
t.goto(-65,135)
rectangle(40,5,'black')
t.hideturtle()








回复 支持 反对

使用道具 举报

1

主题

10

帖子

10

积分

贫民

积分
10
ych  楼主| 发表于 2022-8-1 21:58:19 | 显示全部楼层
import random
import turtle as t
def get_line_length():
    choice=input('Enter line length(long,medium,short):')
    if choice=='long':
        line_length=250
    elif choice=='medium':
        line_length=200
    else:
        line_length=100
    return line_length
def get_line_width():
    choice=input('Enter line length(superthick,thick,short):')
    if choice=='superthick':
        line_width=40
    elif choice=='thick':
        line_width=25
    else:
        line_width=10
    return line_width
line_length=get_line_length()
line_width=get_line_width()
t.shape('turtle')
t.fillcolor('green')
t.bgcolor=('black')
t.speed=('fast')
t.pensize(line_width)
def inside_window():
    left_limit=(-t.window_width()/2)+100
    right_limit=(t.window_width()/2)-100
    top_limit=(t.window_height()/2)-100
    bottom_limit=(-t.window_height()/2)+100
    (x,y)=t.pos()
    inside=left_limit<x<right_limit and bottom_limit<y<top_limit
    return inside
def move_turtle(line_length):
    pen_colors=['red','orange','yellow','green','blue','purple']
    t.pencolor(random.choice(pen_colors))
    if inside_window():
        angle=random.randint(0,180)
        t.right(angle)
        t.forward(line_length)
    else:
        t.backward(line_length)
while True:
    move_turtle(line_length)
line_length=get_line_length()



























        

回复 支持 反对

使用道具 举报

1

主题

10

帖子

10

积分

贫民

积分
10
ych  楼主| 发表于 2022-8-1 22:03:03 | 显示全部楼层
from tkinter import Tk,Canvas
from datetime import date,datetime
def get_events():
    list_events=[]
    with open('events.txt')as file:
        for line in file:
            line=line.rstrip('\n')
            current_event=line.split(',')
            event_date=datetime.strptime(current_event[1],'%d/%m/%y').date()
            print(event_date)
            current_event[1]=event_date
            list_events.append(current_event)
    return list_events
def days_between_days(date1,date2):
    time_between=str(date2-date1)
    number_of_days=time_between.split(' ')
    return number_of_days[0]

root=Tk()
c=Canvas(root,width=800,height=800,bg='black')
c.pack()
c.create_text(100,50,anchor='w',fill='orange',\
font='Arial 28 bold underline',text='My Countdown Calendar')
events=get_events()
today=date.today()
vertical_space=100
for event in events:
    event_name=event[0]
    days_until=days_between_days(event[1],today)
    display='It is %s days until %s'%(days_until,event_name)
    c.create_text(100,vertical_space,anchor='w',fill='lightblue',\
                  font='Arial 28 bold',text=display)
    vertical_space=vertical_space+30
回复 支持 反对

使用道具 举报

1

主题

10

帖子

10

积分

贫民

积分
10
ych  楼主| 发表于 2022-8-1 22:08:45 | 显示全部楼层
from tkinter import Tk,simpledialog,messagebox
def read_from_file():
    with open('capital_data.txt') as file:
        for line in file:
            line=line.rstrip('\n')
            country,city=line.split('/')
            the_world[country]=city
def write_to_file(country_name,city_name):
    with open('capital_data.txt','a')as file:
        file.write('\n'+country_name+'/'+city_name)

print('Ask the Expert-Capital Cities of the world')
root=Tk()
root.withdraw()
the_world={}

read_from_file()
while True:
    query_country=simpledialog.askstring('Country','Type the name of a country')
    if query_country in the_world:
        result=the_world[query_country]
        messagebox.showinfo('Answer'
                            'The capital city of'+query_country+'is'+result+'!')
    else:
         new_city=simpledialog.askstring('Teach me',
                                         'I don’t know'+
                                         'what is the capital of'+query_country+'?')
         the_world[query_country]=new_city
         write_to_file(query_country,new_city)
root.mainloop()






回复 支持 反对

使用道具 举报

1

主题

10

帖子

10

积分

贫民

积分
10
ych  楼主| 发表于 2022-8-1 22:13:05 | 显示全部楼层
from tkinter import messagebox,simpledialog,Tk
from random import choice
def get_task():
    task=simpledialog.askstring('Task','Do you want to encrypt or decrypt?')
    return task
def get_message():
    message=simpledialog.askstring('Message','Enter the secret message:')
    return message
def is_even(number):
    return number%2==0
def get_even_letters(message):
    even_letters=[]
    for counter in range(0,len(message)):
        if is_even(counter):
            even_letters.append(message[counter])
    return even_letters
def get_odd_letters(message):
    odd_letters=[]
    for counter in range(0,len(message)):
        if not is_even(counter):
            odd_letters.append(message[counter])
    return odd_letters
def swap_letters(message):
    letter_list=[]
    if not is_even(len(message)):
        message=message+'x'
    even_letters=get_even_letters(message)
    odd_letters=get_odd_letters(message)
    for counter in range(0,int(len(message)/2)):
        letter_list.append(odd_letters[counter])
        letter_list.append(even_letters[counter])
    new_message=''.join(letter_list)
    return new_message
def encrypt(message):
    encrypted_list=[]
    fake_letters=['a','b','c','d','e','f','g','h','i','j','k','m','n','x','y','z']
    for counter in range(0,len(message)):
        encrypted_list.append(message[counter])
        encrypted_list.append(choice(fake_letters))
    new_message=''.join(encrypted_list)
    return new_message


def decrypt(message):
    even_letters=get_even_letters(message)
    new_message=''.join(even_letters)
    return new_message


root=Tk()
while True:
    task=get_task()
    if task=='encrypt':
        message=get_message()
        encrypted=encrypt(message)
        messagebox.showinfo('',encrypted)
    elif task=='decrypt':
        message=get_message()
        decrypted=decrypt(message)
        messagebox.showinfo('',decrypted)
    else:
        break
root.mainloop()
回复 支持 反对

使用道具 举报

1

主题

10

帖子

10

积分

贫民

积分
10
ych  楼主| 发表于 2022-8-2 22:29:05 | 显示全部楼层
from itertools import cycle
from random import randrange
from tkinter import Tk,Canvas,messagebox,font
Canvas_width=800
Canvas_height=400
root=Tk()
c=Canvas(root,width=Canvas_width,height=Canvas_height,background='deep sky blue')
c.create_rectangle(-5,Canvas_height-100,Canvas_width+5,Canvas_height+5,fill='sea green')
c.create_oval(-80,-80,120,120,fill='orange',width=0)
c.pack()
color_cycle=cycle(['light blue','light green','light pink','light yellow','light cyan'])
egg_width=45
egg_height=55
egg_score=10
egg_speed=500
egg_interval=4000
difficulty_factor=0.95
catcher_color='blue'
catcher_height=100
catcher_width=100
catcher_start_x=Canvas_width/2-catcher_width/2
catcher_start_y=Canvas_height/2-catcher_height+140
catcher_start_x2=catcher_start_x+catcher_width
catcher_start_y2=catcher_start_y+catcher_height
catcher=c.create_arc(catcher_start_x,catcher_start_y,catcher_start_x2,catcher_start_y2,\
                     start=200,extent=140,style='arc',outline=catcher_color,width=3)
game_font=font.nametofont('TkFixedFont')
game_font.config(size=18)
score=0
score_text=c.create_text(10,10,anchor='nw',font=game_font,fill='darkblue',text='Score:'+str(score))
living_remaining=3
live_text=c.create_text(Canvas_width-10,10,anchor='ne',font=game_font,\
                        fill='darkblue',text='Lives:'+str(living_remaining))
eggs=[]
def create_eggs():
    x=randrange(10,740)
    y=40
    new_egg=c.create_oval(x,y,x+egg_width,y+egg_height,fill=next(color_cycle),width=0)
    eggs.append(new_egg)
    root.after(egg_interval,create_eggs)
def move_eggs():
    for egg in eggs:#循环经过所有的蛋
        (egg_x,egg_y,egg_x2,egg_y2)=c.coords(egg)#获取蛋的坐标
        c.move(egg,0,10)
        if egg_y2>Canvas_height:
            egg_dropped(egg)
    root.after(egg_speed,move_eggs)
def egg_dropped(egg):
    eggs.remove(egg)
    c.delete(egg)
    lose_a_life()
    if living_remaining==0:
        messagebox.showinfo('Game over!','Final score:'+str(score))
        root.destroy()#游戏结束
def lose_a_life():
    global living_remaining
    living_remaining-=1
    c.itemconfigure(live_text,text='Lives:'+str(living_remaining))
def check_catch():
    (catcher_x,catcher_y,catcher_x2,catcher_y2)=c.coords(catcher)
    for egg in eggs:
        (egg_x,egg_y,egg_x2,egg_y2)=c.coords(egg)
        if catcher_x<egg_x and catcher_x2>egg_x2 and catcher_y2-egg_y2<40:
            eggs.remove(egg)
            c.delete(egg)
            increase_score(egg_score)
    root.after(100,check_catch)
def increase_score(points):
    global score,egg_speed,egg_interval
    score+=points
    egg_speed=int(egg_speed*difficulty_factor)
    egg_interval=int(egg_interval*difficulty_factor)
    c.itemconfigure(score_text,text='Score:'+str(score))
def move_left(event):
    (x1,y1,x2,y2)=c.coords(catcher)
    if x1>0:
        c.move(catcher,-20,0)
def move_right(event):
    (x1,y1,x2,y2)=c.coords(catcher)
    if x2<Canvas_width:
        c.move(catcher,20,0)
c.bind('<Left>',move_left)
c.bind('<Right>',move_right)
c.focus_set()
root.after(1000,create_eggs)
root.after(1000,move_eggs)
root.after(1000,check_catch)
root.mainloop()
  
回复 支持 反对

使用道具 举报

1

主题

10

帖子

10

积分

贫民

积分
10
ych  楼主| 发表于 2022-8-2 23:25:39 | 显示全部楼层

import matplotlib.pyplot as p#引入图片
import matplotlip.image as i
cat=i.imread('机器猫.jpg')
p.imshow(cat)
p.show()

button_symbols={}#设置按钮
symbols=[u'\u2702']
random.shuffle(symbols)
for x in range(6):
    for y in range(4)
        button=Button(command=lambda x=x,y=y:show_symbol(x,y),\
                      width=3,height=3
        button.grid(column=x,row=y)
        button[x,y]=button
        button_symbols[x,y]=symbols.pop()
import time#设置声音
from pygame import mixer
mixer.init()
beep=mixer.sound("beep.wav")
beep.play()
time.sleep(5)

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表