dejio 2022-04-10 13:57 采纳率: 100%
浏览 44
已结题

代码没有任何问题,但是一直在报错,不知道是什么问题

我没事写了个代码,一运行就一直报错
代码如下

# -*- coding:utf-8 -*-
import pygame as p
import sys as s

result = input("Welcome!If you want to play the game,please enter 1,if you want to know the writer,please enter 2 ,if you want to exit,please enter 3,if you want to experience other functions,please enter 4")
if result == 1:
    def game():
        p.init()
        size = width,height = 500,400
        screen = p.display.set_mode(size)
        color = (0,0,0)
        ball = p.image.load("ball(1).jpg")
        ballrect = ball.get_rect()

        speed = [5,5]
        clock = p.time.Clock()
        while True:
            clock.tick(60)
            for event in p.event.get():
                if event.type == p.QUIT:
                    s.exit()

            ballrect = ballrect.move(speed)
            if ballrect.left < 0 or ballrect.right > width:
                speed[0] = -speed[0]
            if ballrect.top < 0 or ballrect.bottom > height:
                speed[1] = -speed[1]

            screen.fill(color)
            screen.blit(ball,ballrect)
            p.display.flip()

        p.quit()
    game()
if result == 2:
    print("Writer:ekl      All Rights Reserved.")
if result == 3:
    exit()
else:
    result = input("the functions are developmenting and they're dangerous,Sure to use it?(Yes or No)")
    if result == "Yes":                                                                                                                   #有问题
        result = input("Plase choose the experiences   1.command    2.games      3.exit           4..........(only enter the numbers)")
    if result == 1:
        print("please write the command")
        result = input("e.g:exit()")
    if result == "exit()":
        exit()
    if result == "game()":
        game()
    else:
        print("the command is wrong,the program will exit")
        exit()
    if result == 2:
        pass
    if result == 3:
        exit()
    else:
        pass
    if result == "No":                                                                                                                  #同样有问题
        exit()

运行结果:

C:\Python27\python.exe C:/Users/Administrator/Desktop/pythonProject5/add.py
pygame 2.0.3 (SDL 2.0.16, Python 2.7.18)
Hello from the pygame community. https://www.pygame.org/contribute.html
Welcome!If you want to play the game,please enter 1,if you want to know the writer,please enter 2 ,if you want to exit,please enter 3,if you want to experience other functions,please enter 44
the functions are developmenting and they're dangerous,Sure to use it?(Yes or No)No
Traceback (most recent call last):
  File "C:/Users/Administrator/Desktop/pythonProject5/add.py", line 40, in <module>
    result = input("the functions are developmenting and they're dangerous,Sure to use it?(Yes or No)")
  File "<string>", line 1, in <module>
NameError: name 'No' is not defined

Process finished with exit code 1


一直找不到问题

  • 写回答

1条回答 默认 最新

  • gitis 2022-04-10 14:05
    关注

    input()讀取的值爲str類型, 你if的條件判斷好幾處錯誤, 應是result=='2'這樣的形式,
    你先修改這个試試

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月20日
  • 已采纳回答 4月12日
  • 创建了问题 4月10日

悬赏问题

  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来