所以,我只是做了这个怪异的程序由于无聊,称为“Flex机器”(它仍在工作),显示一些简单的动画,游戏和; 我一个接一个做的项目。 在第一位,它会询问您是否想要看到它,但是当您说是时,它会在ELIF上显示语法错误。 有人能帮我解决这个问题并解释一下是什么问题引起的吗?
代码:
import pygame
import random
import time
# Begin the Flex
print("Yo! What's Up?")
time.sleep(2)
print("So uh since you have opened this, I will tell you what it's about")
time.sleep(2)
print("My creator got hella bored one day and found himself just scrolling through his phone too much. The only option he had was to create me")
time.sleep(2)
answer = input("So, here I am. Shall we continue? Yes or No ")
if answer == "Yes":
print("Let's watch this cool animation that my owner gave me!")
# Snowflake.py
# Initialize the game engine
pygame.init()
BLACK = [0, 0, 0]
WHITE = [255, 255, 255]
BLUE = [0, 0, 255]
# Set the height and width of the screen
SIZE = [400, 400]
screen = pygame.display.set_mode(SIZE)
pygame.display.set_caption("Snow Animation")
# Create an empty array
snow_list = []
# Loop 50 times and add a snow flake in a random x,y position
for i in range(50):
x = random.randrange(0, 400)
y = random.randrange(0, 400)
snow_list.append([x, y])
clock = pygame.time.Clock()
# Loop until the user clicks the close button.
done = False
while not done:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
# Set the screen background
screen.fill(BLACK)
# Process each snow flake in the list
for i in range(len(snow_list)):
# Draw the snow flake
pygame.draw.circle(screen, WHITE, snow_list[i], 2)
# Move the snow flake down one pixel
snow_list[i][1] += 9
# If the snow flake has moved off the bottom of the screen
if snow_list[i][1] > 400:
# Reset it just above the top
y = random.randrange(-50, -10)
snow_list[i][1] = y
# Give it a new x position
x = random.randrange(0, 400)
snow_list[i][0] = x
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
clock.tick(20)
pygame.quit()
elif answer == "No":
print("Well, that's fine")
time.sleep(2)
print("Cya next time, have a good day")
else:
print("Please enter yes or no.")
检查第一个pygame.init()到ELIF之间的缩进。
只有当if条件在以上时,您才必须使用elif! 比如。
if (a == 1):
//do something
elif (a == 1):
//do something
你不能这样做:
elif (a == 1):
//do something
要解决您的问题,请通过if删除您的第一个elif!
if answer == "No":
print("Well, that's fine")
time.sleep(2)
print("Cya next time, have a good day")
else:
print("Please enter yes or no.")
在代码中,有一个if
语句,一个elif
语句和一个else
语句。 您已经正确缩进了elif
和else
语句中的代码,但似乎忘记了缩进if
语句中的代码。 在python中需要缩进,因此您必须缩进if
语句中的代码。
我已经缩进了语句中的代码,所以它现在应该可以正常工作了:
import pygame
import random
import time
# Begin the Flex
print("Yo! What's Up?")
time.sleep(2)
print("So uh since you have opened this, I will tell you what it's about")
time.sleep(2)
print("My creator got hella bored one day and found himself just scrolling through his phone too much. The only option he had was to create me")
time.sleep(2)
answer = input("So, here I am. Shall we continue? Yes or No ")
if answer == "Yes":
print("Let's watch this cool animation that my owner gave me!")
# Snowflake.py
# Initialize the game engine
pygame.init()
BLACK = [0, 0, 0]
WHITE = [255, 255, 255]
BLUE = [0, 0, 255]
# Set the height and width of the screen
SIZE = [400, 400]
screen = pygame.display.set_mode(SIZE)
pygame.display.set_caption("Snow Animation")
# Create an empty array
snow_list = []
# Loop 50 times and add a snow flake in a random x,y position
for i in range(50):
x = random.randrange(0, 400)
y = random.randrange(0, 400)
snow_list.append([x, y])
clock = pygame.time.Clock()
# Loop until the user clicks the close button.
done = False
while not done:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
# Set the screen background
screen.fill(BLACK)
# Process each snow flake in the list
for i in range(len(snow_list)):
# Draw the snow flake
pygame.draw.circle(screen, WHITE, snow_list[i], 2)
# Move the snow flake down one pixel
snow_list[i][1] += 9
# If the snow flake has moved off the bottom of the screen
if snow_list[i][1] > 400:
# Reset it just above the top
y = random.randrange(-50, -10)
snow_list[i][1] = y
# Give it a new x position
x = random.randrange(0, 400)
snow_list[i][0] = x
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
clock.tick(20)
pygame.quit()
elif answer == "No":
print("Well, that's fine")
time.sleep(2)
print("Cya next time, have a good day")
else:
print("Please enter yes or no.")
问题内容: 我可以编译上面的方法。关于允许的多个“ +”运算符,是否有任何解释? 问题答案: 它是加法,然后一元加运算符重复。等效于以下内容:
我正试图在我的本机代码中初始化CI的函数。 我得到一个错误的解析错误:语法错误,意外的[ 我能问一下如何解决这个问题吗? 使用PHP5.3.3
我试图让if语句从多个条件触发,而不使用不同的触发器多次重写该语句。例如。: 你会如何把这些浓缩成一个陈述? 我尝试过使用'or',它导致任何原始输入触发语句,而不管输入是否匹配任一条件。
每种语言都有自己的语法,不管是自然语言(英语,中文)还是计算机编程语言。 Python 也不例外,它也有自己的语法规则,然后编辑器或者解析器根据符合语法的程序代码转换成 CPU 能够执行的机器码,然后执行。 Python 的语法比较简单,采用缩进方式。 如上面的代码截图,以 # 开头的语句是注释,其他每一行都是一个语句,当语句以冒号 : 结尾时,缩进的语句视为代码块。 要注意的是 Python 程
问题内容: 我试图获得一个if语句从多个条件中触发,而不用不同的触发器多次重写该语句。例如: 您如何将这些内容浓缩成一个陈述? 我试过使用“或”,并且无论输入是否符合任何条件,它都会导致任何raw_input触发语句。 问题答案: 你想做的是 如果您有很多可能性,请选择另一个选项: 由于您使用的是2.7,因此您也可以这样编写(在2.7或3+版本中有效,但在2.6或以下版本中无效): 这使元素成为您
你好,我有以下问题 查询在我的localhost(XAMMP)中运行良好,但在其他任何地方它都会给我这个错误 示例:http://sqlfiddle.com/#!9/e19e8c/2
问题内容: 您如何在一个日期稀疏的表与另一个日期详尽的表之间进行联接,以使稀疏日期之间的间隔取前一个稀疏日期的值? 说明性示例: 所需结果: 更新: 几个人提出了可以完成所需结果的相关子查询。(相关子查询=包含对外部查询的引用的子查询。) 这将起作用;但是,我应该注意到,我使用的平台是MySQL,其相关子查询的优化效果很差。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。