您好,欢迎来到花图问答。
搜索
您的当前位置:首页python好玩的短代码

python好玩的短代码

来源:花图问答
python好玩的短代码

使用 Python 实现一个简单的摇一摇游戏 import random import time

def shake():

result = random.randint(1, 6) print(\"You shook:\ time.sleep(1)

while True:

shake_input = input(\"Shake the dice? (Y/N)\") if shake_input.upper() == \"Y\": shake()

elif shake_input.upper() == \"N\": print(\"Thanks for playing!\") break else:

print(\"Invalid Input\")

使用 Python 实现一个简单的猜单词游戏

import random

words = [\"python\word = random.choice(words)

guesses = [] maxfails = 7 fails = 0

while fails < maxfails:

guess = input(\"Guess a letter: \").lower() if guess in word:

guesses.append(guess)

print(\" \".join(letter if letter in guesses else \"_\" for letter in word)) else:

fails += 1

print(\"Incorrect. You have\

if \"_\" not in \" \".join(letter if letter in guesses else \"_\" for letter in word): print(\"You won!\") break

if fails == maxfails:

print(\"You lost. The word was\

使用 Python 实现一个简单的电子相册

import os

from PIL import Image

folder = \"your_folder_path\"

for filename in os.listdir(folder):

if filename.endswith(\".jpg\") or filename.endswith(\".png\"): img = Image.open(os.path.join(folder, filename)) img.show()

input(\"Press enter for next image\")

用 Python 实现猜数字游戏 import random

answer = random.randint(1, 100) guess = None

while guess != answer:

guess = int(input(\"Guess a number between 1 and 100: \")) if guess < answer: print(\"Too low!\") elif guess > answer: print(\"Too high!\") else:

print(\"You got it!\")

使用 Python 创建一个简单的聊天机器人 import random

responses = {

\"hi\": [\"Hello!\

\"how are you\": [\"I'm fine, thank you.\ \"bye\": [\"Goodbye!\ }

def respond(message):

if message.lower() in responses:

return random.choice(responses[message.lower()]) else:

return \"I'm sorry, I don't understand.\"

用 Python 画一个乌龟图形 import turtle

t = turtle.Turtle()

for i in range(4): t.forward(100) t.right(90)

turtle.done()

使用 Python 实现一个简单的加密程序 def encrypt(plaintext, key): ciphertext = \"\"

for char in plaintext:

ciphertext += chr(ord(char) + key) return ciphertext

def decrypt(ciphertext, key): plaintext = \"\"

for char in ciphertext:

plaintext += chr(ord(char) - key) return plaintext

message = \"Hello, World!\" key = 5

encrypted_message = encrypt(message, key) print(encrypted_message)

decrypted_message = decrypt(encrypted_message, key) print(decrypted_message)

利用 Python 实现一个简单的文本游戏 def play_game():

story = \"You are trapped in a dark room. There are doors to the North, South, East, and West. Which direction do you want to go?\"

choices = [\"North\ choice = None

while choice not in choices: print(story)

choice = input(\"Enter North, South, East, or West: \").title() if choice == \"North\":

print(\"You find a treasure chest!\") elif choice == \"South\":

print(\"You find a river.\") elif choice == \"East\":

print(\"You find a cave.\") elif choice == \"West\":

print(\"You find a field.\")

play_game()

使用 Python 实现一个简单的画板 import tkinter as tk

def on_left_click(event): x, y = event.x, event.y

canvas.create_oval(x-5, y-5, x+5, y+5, fill=\"black\")

def on_right_click(event): x, y = event.x, event.y

canvas.create_rectangle(x-5, y-5, x+5, y+5, fill=\"white\")

root = tk.Tk()

canvas = tk.Canvas(root, width=300, height=200) canvas.pack()

canvas.bind(\"\ canvas.bind(\"\

root.mainloop()

使用 Python 实现一个简单的数学计算器 def add(x, y): return x + y

def subtract(x, y): return x - y

def multiply(x, y): return x * y

def divide(x, y): return x / y

print(\"Select operation.\") print(\"1.Add\") print(\"2.Subtract\") print(\"3.Multiply\") print(\"4.Divide\")

choice = input(\"Enter choice(1/2/3/4): \") num1 = float(input(\"Enter first number: \")) num2 = float(input(\"Enter second number: \"))

if choice == '1':

print(num1, \"+\

elif choice == '2':

print(num1, \"-\

elif choice == '3':

print(num1, \"*\

elif choice == '4':

print(num1, \"/\ else:

print(\"Invalid Input\")

使用 Python 实现一个简单的电子钟

import time

import tkinter as tk

def time():

current_time = time.strftime(\"%H:%M:%S\") clock.config(text=current_time) clock.after(200, time)

root = tk.Tk()

clock = tk.Label(root, font=(\"calibri\ clock.pack(anchor=\"center\") time()

root.mainloop()

使用 Python 实现一个简单的音乐播放器 import pygame

pygame.init()

pygame.mixer.init()

pygame.mixer.music.load(\"your_music_file.mp3\") pygame.mixer.music.play()

while True:

user_input = input(\"Enter Q to quit:\") if user_input.upper() == \"Q\": pygame.mixer.music.stop() break

使用 Python 实现一个简单的电子抽奖程序

import random

names = [\"John\ winner = random.choice(names)

print(\"The winner is:\

使用 Python 实现一个简单的计算器

def calculator():

operation = input('''

Please type in the math operation you would like to complete: + for addition - for subtraction * for multiplication / for division ''')

number_1 = int(input('Please enter the first number: ')) number_2 = int(input('Please enter the second number: '))

if operation == '+':

print('{} + {} = '.format(number_1, number_2)) print(number_1 + number_2)

elif operation == '-':

print('{} - {} = '.format(number_1, number_2)) print(number_1 - number_2)

elif operation == '*':

print('{} * {} = '.format(number_1, number_2)) print(number_1 * number_2)

elif operation == '/':

print('{} / {} = '.format(number_1, number_2)) print(number_1 / number_2)

else:

print('You have not typed a valid operator, please run the program again.')

calculator()

使用 Python 实现一个简单的记事本程序

def notes():

note = input(\"Enter your note: \") with open(\"notes.txt\ f.write(note + \"\\n\") print(\"Note added.\")

while True:

user_input = input(\"Do you want to add a note? (Y/N)\") if user_input.upper() == \"Y\": notes()

elif user_input.upper() == \"N\": break else:

print(\"Invalid Input\")

使用 Python 实现一个简单的贪吃蛇游戏

import turtle import time

wn = turtle.Screen()

wn.title(\"Simple Snake Game\") wn.bgcolor(\"black\")

wn.setup(width=600, height=600) wn.tracer(0)

head = turtle.Turtle() head.speed(0)

head.shape(\"square\") head.color(\"white\") head.penup() head.goto(0,0)

head.direction = \"stop\"

food = turtle.Turtle() food.speed(0)

food.shape(\"circle\") food.color(\"red\") food.penup() food.goto(0,100)

segments = []

def go_up():

if head.direction != \"down\": head.direction = \"up\"

def go_down():

if head.direction != \"up\": head.direction = \"down\"

def go_left():

if head.direction != \"right\": head.direction = \"left\"

def go_right():

if head.direction != \"left\": head.direction = \"right\"

def move():

if head.direction == \"up\": y = head.ycor() head.sety(y + 20)

if head.direction == \"down\": y = head.ycor() head.sety(y - 20)

if head.direction == \"left\": x = head.xcor() head.setx(x - 20)

if head.direction == \"right\": x = head.xcor() head.setx(x + 20)

wn.listen()

wn.onkeypress(go_up, \"Up\")

wn.onkeypress(go_down, \"Down\") wn.onkeypress(go_left, \"Left\") wn.onkeypress(go_right, \"Right\")

while True: wn.update()

if head.distance(food) < 20:

x = random.randint(-290, 290) y = random.randint(-290, 290) food.goto(x, y)

move()

time.sleep(0.1) wn.mainloop()

这些代码是很有趣的,它们可以帮助你了解 Python 编程的基本概念。当然还有很多玩法,如果有其他问题欢迎继续问我。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuowenda.com 版权所有 湘ICP备2023022495号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务