首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python hangman游戏

Python hangman游戏
EN

Stack Overflow用户
提问于 2015-05-04 08:42:00
回答 1查看 4K关注 0票数 0

我正在用Python开发一个基于图形用户界面的绞刑者游戏。在运行我的代码时,我收到错误:“行连续字符之后的意外字符”,紧跟在我的第一个标签行读作"hangman parts“之后。不确定如何修复。我是Python的新手,一点也不擅长,所以这个程序可能还有很多其他的缺陷。任何建议都将不胜感激。

代码语言:javascript
复制
import tkinter
import tkinter.messagebox
import random

#fruit category
easyWords = ['apple', 'orange', 'mango', 'peach', 'guava']
#space category
mediumWords = ['atmosphere', 'jupiter', 'quasar', 'satellite', 'asteroid']
#science category
hardWords = ['biochemical', 'hemoglobin', 'emulsify', 'reactant', 'dynamo']

def setting():

    wordChoice = ''

    difficulty = input('''Welcome to hangman, select your difficulty.
Type, easy, medium, or hard to begin:''')

    if difficulty == 'easy':

        wordChoice = easyWords.random
        print('You have selected easy mode, start guessing your letters now in the game window. The category is: fruit')

    if difficulty == 'medium':

        wordChoice = mediumWords.random
        print('You have selected medium mode, start guessing your letters now in the game window. The category is: space')

    if difficulty == 'hard':

        wordChoice = hardWords.random
        print('You have selected hard mode, start guessing your letters now in the game window. The category is: science')

def game():

    missGuess = 0
    guesses = ''

    for char in wordChoice:
        label3.print(char),

        if char in guesses:
            print(char),

        else:
            label3.print("_"),
            missGuess += 1

        if missGuess == 1:
            label1.print('head')
        if missGuess == 2:
            label1.print('torso')
        if missGuess == 3:
            label1.print('left arm')
        if missGuess == 4:
            label1.print('right arm')
        if missGuess == 5:
            label1.print('left leg')
        if missGuess == 6:
            label1.print('right leg'),

class MyGUI():
    def __init__(self):
        self.main_window = tkinter.Tk()

        #create needed frames
        self.top_frame = tkinter.Frame(self.main_window)
        self.center_frame = tkinter.Frame(self.main_window)
        self.bottom_frame = tkinter.Frame(self.main_window)

        #create top frame labels
        self.label1 = tkinter.Label(self.top_frame, \ text='Hangman parts:')
        self.label2 = tkinter.Label(self.top_frame, \ text=' ')
        #center frame labels
        self.label3 = tkinter.Label(self.center_frame, \ text=' ')
        #bottom frame labels
        self.label4 = tkinter.Label(self.bottom_frame, \ text='Guess a letter:')
        self.entry1 = tkinter.Entry(self.bottom_frame, \ width=5)
        self.button1 = tkinter.Button(self.bottom_frame, \ text='Guess', command=self.game) #calls the game method
        self.button2 = tkinter.Button(self.bottom_frame, \ text='Exit', \ command=self.main_window.destroy))

        #pack top frame labels
        self.label1.pack(side='left')
        self.label2.pack(side='right')
        #pack center frame
        self.label3.pack(side='top')
        #bottom frame
        self.label4.pack(side='left')
        self.entry1.pack(side='left')
        self.button1.pack(side='left')
        self.button2.pack(side='left')

    tkinter.mainloop()



setting()
main()
EN

回答 1

Stack Overflow用户

发布于 2015-05-04 08:50:40

在你的队伍里

代码语言:javascript
复制
self.label1 = tkinter.Label(self.top_frame, \ text='Hangman parts:')

在Python语言中,您使用的是称为“行连续字符”的反斜杠\

这用于要在换行符上换行的长行,如下所示:

代码语言:javascript
复制
a = 5\
+ 3

但是,在您的代码中,这一行实际上并没有断开,因此只需删除这些行中的反斜杠,如下所示:

代码语言:javascript
复制
self.label1 = tkinter.Label(self.top_frame, text='Hangman parts:')

否则,Python会在它找不到的\后面加上一个换行符--这会导致您看到的错误。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30021069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档