在 Python 中启用退出功能时出现问题

发布于 2024-08-17 23:05:15 字数 4197 浏览 5 评论 0原文

一般来说,我对编程还很陌生,我正在为我的妹妹创建一个小游戏......

我有一个 while 循环,其中我想要一个退出游戏的选项,但我不知道退出技巧似乎有效:

#main game:
while 1:
    input_event_1 = gui.buttonbox(
        msg = 'Hello, what would you like to do with your Potato Head?',
        title = 'Main Screen',
        choices = ('Check Stats', 'Feed', 'Exercise', 'Teach', 'Play', 'Go to Doctor', 'Change Favourite Thing', 'Get New Toy', 'Quit'))
    if input_event_1 == 'Check Stats':
        myPotatoHead.check_p_h_stats()
    if input_event_1 == 'Change Favourite Thing':
        myPotatoHead.change_favourite_thing()
    if input_quit == 'Quit':
        input_quit = gui.ynbox(
        msg = 'Are you sure you want to quit?',
        title = 'Confirm quit',
        choices = ('Quit', 'Cancel'))
        if input_event_quit == 'Quit':
            sys.exit(1)

感谢任何帮助。

谢谢


-----更新-----

感谢您的建议,但它仍然不起作用:

这是更新的代码:

#import the required modules:
import easygui as gui
import os


#-----CLASS-----------------------------------
#Class:
class PotatoHead:

#Atributes:
    def __init__(self):
        self.data = game_data
        self.first_name = self.data[0]
        self.last_name = self.data[1]
        self.gender = self.data[2]
        self.colour = self.data[3]
        self.fav_thing = self.data[4]
        self.toys = []
        self.toys.append(self.data[5])
        self.age = '0.0'
        self.hunger = '0.0'
        self.health = '0.0'
        self.fitness = '0.0'
        self.education = '0.0'
        self.happiness = '0.0'
        self.tiredness = '0.0'

    def check_p_h_stats(self):
        self.toys_string = str(self.toys)
        gui.msgbox(
            msg = '''
Name: ''' + self.first_name + ' ' + self.last_name + '''
Gender: ''' + self.gender + '''
Colour: ''' + self.colour + '''
Favourite Thing: ''' + self.colour + '''
Toys:''' + self.toys_string + '''
Age: ''' + self.age + '''
Hunger: ''' + self.hunger + '''
Health: ''' + self.health + '''
Fitness: ''' + self.fitness + '''
Education: ''' + self.education + '''
Happiness: ''' + self.happiness + '''
Tiredness: ''' + self.tiredness + '''
''',
            title = 'Potato Head Stats',
            ok_button = 'Continue')

    def change_favourite_thing(self):
        new_fav_thing = gui.enterbox(
            msg = 'Enter the new favourite thing:',
            title = 'Change Favourite Thing',
            default = 'Type Here')
        self.fav_thing = new_fav_thing

#Methods:
#-----MAIN PROGRAM----------------------------
#set up game:
image = 'nest.gif'
game_choice = gui.ynbox(
    msg = """Would you like to start a new game,
or load a previously saved one?""",
    title = 'Start/Load Game',
    choices = ('New Game', 'Load Game'),
    image = image)
if game_choice == 1:
    fieldNames = ['First Name', 'Last Name', 'Gender', 'Colour', 'Favourite Thing',     'First Toy']
    new_p_head_data = []
    new_p_head_data = gui.multenterbox(
        msg = 'Fill in the starting information about your Potato Head:',
        title = 'New Game',
        fields = fieldNames,
        values = ('', '', 'Male/Female', 'Red, Green, Blue, Yellow, White, Black', '',     'Choose either Rattle, Pacifier, Teddy, Doll, or Soft Ball'))
    game_data = new_p_head_data
else:
    gui.msgbox('This function is not yet supported, please start again...')

myPotatoHead = PotatoHead()

#main game:
while 1:
    input_event_1 = gui.buttonbox(
        msg = 'Hello, what would you like to do with your Potato Head?',
        title = 'Main Screen',
        choices = ('Check Stats', 'Feed', 'Exercise', 'Teach', 'Play', 'Go to Doctor', 'Change Favourite Thing', 'Get New Toy', 'Quit'))
    if input_event_1 == 'Check Stats':
        myPotatoHead.check_p_h_stats()
    elif input_event_1 == 'Change Favourite Thing':
        myPotatoHead.change_favourite_thing()
    elif input_event_1 == 'Quit':
        input_quit = gui.ynbox(
            msg = 'Are you sure you want to quit?',
            title = 'Confirm quit',
            choices = ('Quit', 'Cancel'))
        if input_quit == 'Quit':
            sys.exit(1)

Am using PYthon 2.5.4 for Mac 我

再次使用 Easygui 0.83,感谢您的建议

I'm pretty new to programming in general and I'm creating a small game for my younger sister...

I have a while loop in which I want to have an option to quit the game, but none of the quitting techniques I know of seem to work:

#main game:
while 1:
    input_event_1 = gui.buttonbox(
        msg = 'Hello, what would you like to do with your Potato Head?',
        title = 'Main Screen',
        choices = ('Check Stats', 'Feed', 'Exercise', 'Teach', 'Play', 'Go to Doctor', 'Change Favourite Thing', 'Get New Toy', 'Quit'))
    if input_event_1 == 'Check Stats':
        myPotatoHead.check_p_h_stats()
    if input_event_1 == 'Change Favourite Thing':
        myPotatoHead.change_favourite_thing()
    if input_quit == 'Quit':
        input_quit = gui.ynbox(
        msg = 'Are you sure you want to quit?',
        title = 'Confirm quit',
        choices = ('Quit', 'Cancel'))
        if input_event_quit == 'Quit':
            sys.exit(1)

grateful for any help.

Thanks


-----UPDATE-----

Thanks for the advice, but it's still not working:

here's the updated code:

#import the required modules:
import easygui as gui
import os


#-----CLASS-----------------------------------
#Class:
class PotatoHead:

#Atributes:
    def __init__(self):
        self.data = game_data
        self.first_name = self.data[0]
        self.last_name = self.data[1]
        self.gender = self.data[2]
        self.colour = self.data[3]
        self.fav_thing = self.data[4]
        self.toys = []
        self.toys.append(self.data[5])
        self.age = '0.0'
        self.hunger = '0.0'
        self.health = '0.0'
        self.fitness = '0.0'
        self.education = '0.0'
        self.happiness = '0.0'
        self.tiredness = '0.0'

    def check_p_h_stats(self):
        self.toys_string = str(self.toys)
        gui.msgbox(
            msg = '''
Name: ''' + self.first_name + ' ' + self.last_name + '''
Gender: ''' + self.gender + '''
Colour: ''' + self.colour + '''
Favourite Thing: ''' + self.colour + '''
Toys:''' + self.toys_string + '''
Age: ''' + self.age + '''
Hunger: ''' + self.hunger + '''
Health: ''' + self.health + '''
Fitness: ''' + self.fitness + '''
Education: ''' + self.education + '''
Happiness: ''' + self.happiness + '''
Tiredness: ''' + self.tiredness + '''
''',
            title = 'Potato Head Stats',
            ok_button = 'Continue')

    def change_favourite_thing(self):
        new_fav_thing = gui.enterbox(
            msg = 'Enter the new favourite thing:',
            title = 'Change Favourite Thing',
            default = 'Type Here')
        self.fav_thing = new_fav_thing

#Methods:
#-----MAIN PROGRAM----------------------------
#set up game:
image = 'nest.gif'
game_choice = gui.ynbox(
    msg = """Would you like to start a new game,
or load a previously saved one?""",
    title = 'Start/Load Game',
    choices = ('New Game', 'Load Game'),
    image = image)
if game_choice == 1:
    fieldNames = ['First Name', 'Last Name', 'Gender', 'Colour', 'Favourite Thing',     'First Toy']
    new_p_head_data = []
    new_p_head_data = gui.multenterbox(
        msg = 'Fill in the starting information about your Potato Head:',
        title = 'New Game',
        fields = fieldNames,
        values = ('', '', 'Male/Female', 'Red, Green, Blue, Yellow, White, Black', '',     'Choose either Rattle, Pacifier, Teddy, Doll, or Soft Ball'))
    game_data = new_p_head_data
else:
    gui.msgbox('This function is not yet supported, please start again...')

myPotatoHead = PotatoHead()

#main game:
while 1:
    input_event_1 = gui.buttonbox(
        msg = 'Hello, what would you like to do with your Potato Head?',
        title = 'Main Screen',
        choices = ('Check Stats', 'Feed', 'Exercise', 'Teach', 'Play', 'Go to Doctor', 'Change Favourite Thing', 'Get New Toy', 'Quit'))
    if input_event_1 == 'Check Stats':
        myPotatoHead.check_p_h_stats()
    elif input_event_1 == 'Change Favourite Thing':
        myPotatoHead.change_favourite_thing()
    elif input_event_1 == 'Quit':
        input_quit = gui.ynbox(
            msg = 'Are you sure you want to quit?',
            title = 'Confirm quit',
            choices = ('Quit', 'Cancel'))
        if input_quit == 'Quit':
            sys.exit(1)

Am using PYthon 2.5.4 for Mac
and am using Easygui 0.83

Again, thanks for any advice

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

悲念泪 2024-08-24 23:05:15

我认为这是你的问题:

if input_quit == 'Quit':
        input_quit = gui.ynbox(
        msg = 'Are you sure you want to quit?',

应该是

if input_event_1 == 'Quit':
        input_quit = gui.ynbox(
        msg = 'Are you sure you want to quit?',

编辑:根据 EasyGui 教程,它仍然不起作用的原因是 ynbox 返回 0 或 1,而不是字符串选择的价值。所以改为

    elif input_event_1 == 'Quit':
        input_quit = gui.ynbox(
            msg = 'Are you sure you want to quit?',
            title = 'Confirm quit',
            choices = ('Quit', 'Cancel'))
        if input_quit == 1:
            sys.exit(1)

I think this is your problem:

if input_quit == 'Quit':
        input_quit = gui.ynbox(
        msg = 'Are you sure you want to quit?',

should be

if input_event_1 == 'Quit':
        input_quit = gui.ynbox(
        msg = 'Are you sure you want to quit?',

Edit: the reason it's still not working, according to the EasyGui tutorial, is that ynbox returns 0 or 1, rather than the string values of the choices. So change to

    elif input_event_1 == 'Quit':
        input_quit = gui.ynbox(
            msg = 'Are you sure you want to quit?',
            title = 'Confirm quit',
            choices = ('Quit', 'Cancel'))
        if input_quit == 1:
            sys.exit(1)
小兔几 2024-08-24 23:05:15

我想也许你的代码

 if input_quit == 'Quit':

应该

 if input_event_1 == 'Quit':

像其他检查一样?我不熟悉那个 buttonbox 东西,但是这个检查与其他检查之间的差异立即令人震惊并跳入眼帘......

编辑:啊,找到了它,它来自 easygui (提及您正在使用的框架是有帮助!-) -- 是的,它返回与按钮关联的字符串,所以我上面建议的更改绝对是关键点。

另外,将第一个之后的所有 if input_event_1 更改为 elif input_event_1 (因为它不能等于多个字符串,为什么在找到后还要继续检查它确实等于的一个字符串?-)虽然不是绝对必要的,但将是一种改进。

I think maybe your code

 if input_quit == 'Quit':

should be

 if input_event_1 == 'Quit':

just like your other checks? I'm not familiar with that buttonbox thingy but the discrepancy between this one check and the others is immediately jarring and jumps to the eye...

Edit: ah, found it, it's from easygui (mentioning what frameworks you're using would be helpful!-) -- yep, it returns the string associated to the button, so the change I suggested above is definitely the key point.

Also, changing all the if input_event_1 after the first one to elif input_event_1 (since it can't be equal to more than one string, why keep checking once you've found the one string it does equal?-), while not strictly necessary, would be an improvement.

苏别ゝ 2024-08-24 23:05:15

这种方式更优雅:

she_wants_to_play = true
while she_wants_to_play:
    ask input
    if input == quit:
        she_wants_to_play = false

所以你很好地退出了 while 循环,而不是用 10000 吨的重量撞击系统。

It is more elegant this way:

she_wants_to_play = true
while she_wants_to_play:
    ask input
    if input == quit:
        she_wants_to_play = false

So you nicely quit the while loop, instead of hitting the system with a 10000 Ton weight.

小伙你站住 2024-08-24 23:05:15

很少的检查点

  • 你导入了 sys 吗?
  • if input_event_quit == 'Quit': 应该是 if input_quit == 'Quit':

Few checkpoints

  • Did you import sys?
  • if input_event_quit == 'Quit': should be if input_quit == 'Quit':
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文