[英]Python hangman game
我正在使用Python開發基於GUI的子手游戲。 運行我的代碼后,在我的第一個標簽行顯示為“ hangman parts”之后,我收到錯誤消息:“連續行字符后出現意外字符”。 不知道如何解決。 我是Python新手,一點也不擅長,因此該程序可能還有許多其他缺陷。 任何提示將非常感謝。
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()
在你的行
self.label1 = tkinter.Label(self.top_frame, \ text='Hangman parts:')
您使用的反斜杠\\
在Python中稱為“行繼續符”。
這用於想要在換行符之間打斷的長行,如下所示:
a = 5\
+ 3
但是,在您的代碼中,該行實際上並未斷開,因此只需在這些行中刪除反斜杠即可,如下所示:
self.label1 = tkinter.Label(self.top_frame, text='Hangman parts:')
否則,Python會在\\
后面尋找換行符,該換行符找不到-並導致您看到的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.