一、安装xlrd出现错误
1.问题描述
在使用Python操作excel时,需要用到xlrd这个库,但是在安装xlrd时,会出现如下错误提示:
Command "python setup.py egg_info" failed with error code 1 in C:\Users\XXX\AppData\Local\Temp\pip-install-u1_sa0xl\xlrd\
2.问题分析
这个问题多半是由于网络原因引起的,因为大部分情况下,都是无法连接到pypi.org或者连接被中断导致的。也有很多人反映这是因为setuptools版本不太对引起的问题。
3.解决方法
方法一:升级setuptools版本
在terminal/command prompt里输入以下命令,然后重新安装xlrd:
pip install --upgrade setuptools
方法二:设置国内镜像
在terminal/command prompt里输入以下命令:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xlrd
二、Python简单小游戏代码大全
Python是一门高级语言,非常适合编写小游戏。以下是一些Python简单小游戏代码大全供参考:
1.猜数字游戏(Guess the Number)
思路:让玩家猜一个随机生成的数字。
Python代码:
import random
guessesTaken = 0
print('Hello! What is your name?')
myName = input()
number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')
while guessesTaken < 6:
print('Take a guess.')
guess = input()
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < number:
print('Your guess is too low.')
if guess > number:
print('Your guess is too high.')
if guess == number:
break
if guess == number:
guessesTaken = str(guessesTaken)
print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')
if guess != number:
number = str(number)
print('Nope. The number I was thinking of was ' + number)
2.猜单词游戏(Guess the Word)
思路:从一个列表中随机选择一个单词,让玩家猜这个单词是什么。
Python代码:
import random
WORDS = ['python', 'java', 'ruby', 'csharp', 'php']
word = random.choice(WORDS)
correct = word
jumble = ''
while word:
position = random.randrange(len(word))
jumble += word[position]
word = word[:position] + word[(position + 1):]
print('The jumble is:', jumble)
guess = input('Your guess: ')
while guess != correct and guess != '':
print('Sorry, that is not it.')
guess = input('Your guess: ')
if guess == correct:
print('You guessed it! Congratualations!')
3.贪吃蛇游戏(Snake Game)
思路:编写一个小程序,模拟贪吃蛇的游戏过程。
Python代码:
import turtle
import time
import random
delay = 0.1
#Score
score = 0
high_score = 0
wn = turtle.Screen()
wn.title("Snake Game by @Anson")
wn.bgcolor("green")
wn.setup(width=600, height=600)
#Snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0,0)
head.direction = "stop"
#Snake food
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
food.goto(0,100)
segments = []
#Scoreboard
pen = turtle.Turtle()
pen.speed(10)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 250)
pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))
#Functions
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)
#Keyboard bindings
wn.listen()
wn.onkeypress(go_up, "Up")
wn.onkeypress(go_down, "Down")
wn.onkeypress(go_left, "Left")
wn.onkeypress(go_right, "Right")
#Main game loop
while True:
wn.update()
#Check for collision with border
if head.xcor()>290 or head.xcor()<-290 or head.ycor()>290 or head.ycor()<-290:
time.sleep(1)
head.goto(0,0)
head.direction = "stop"
#Hide the segments
for segment in segments:
segment.goto(1000, 1000)
#Clear the segments list
segments.clear()
#Reset the score
score = 0
#Reset the delay
delay = 0.1
pen.clear()
pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))
#Check for collision with food
if head.distance(food) < 20:
#Move the food to a random spot
x = random.randint(-290, 290)
y = random.randint(-290, 290)
food.goto(x,y)
#Add a segment
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("grey")
new_segment.penup()
segments.append(new_segment)
#Shorten the delay
delay -= 0.001
#Increase the score
score += 10
if score > high_score:
high_score = score
pen.clear()
pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))
#Move the end segments first in reverse order
for index in range(len(segments)-1, 0, -1):
x = segments[index-1].xcor()
y = segments[index-1].ycor()
segments[index].goto(x, y)
#Move segment 0 to where the head is
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x,y)
move()
#Check for head collision with the body
for segment in segments:
if segment.distance(head) < 20:
time.sleep(1)
head.goto(0,0)
head.direction = "stop"
for segment in segments:
segment.goto(1000, 1000)
segments.clear()
score = 0
delay = 0.1
pen.clear()
pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))
time.sleep(delay)
wn.mainloop()
以上就是Python简单小游戏代码大全,希望对大家有所帮助! 如果你喜欢我们三七知识分享网站的文章, 欢迎您分享或收藏知识分享网站文章 欢迎您到我们的网站逛逛喔!https://www.ynyuzhu.com/
发表评论 取消回复