Trong game này, bạn sẽ học các khái niệm về:
- Methods – Phương thức
- The append() list method – Thêm thành phần của list (danh sách)
- The lower() and upper() string methods – phương thức chữ thường và chữ hoa cho chuỗi
- The reverse() list method – phương thức đảo danh sách
- The split() string method – phương thức chia chuỗi
- The range() function – hàm range()
- The list() function – hàm list()
- for loops – vòng lặp loop
- elif statements – Kiểm tra điều kiện
- The startswith() and endswith() string methods. – Phương thức startswith() và endswith()
- The dictionary data type. – Kiểu dữ liệu tử điển
- key-value pairs – Cặp khóa – giá trị
- The keys() and values() dictionary methods – phương thức keys() và values()
- Multiple variable assignment, such as a, b, c = [1, 2, 3] – Đa gán
![8-1](http://hocbaomat.com/wp-content/uploads/2012/08/8-1.png)
Còn code của game thì đây:
import random HANGMANPICS = [''' +---+ | | | | | | =========''', ''' +---+ | | O | | | | =========''', ''' +---+ | | O | | | | | =========''', ''' +---+ | | O | /| | | | =========''', ''' +---+ | | O | /|\ | | | =========''', ''' +---+ | | O | /|\ | / | | =========''', ''' +---+ | | O | /|\ | / \ | | ========='''] words = 'hocbaomat linux hacker backtrack python wordpress javascript'.split() def getRandomWord(wordList): # This function returns a random string from the passes list of strings. wordIndex = random.randint(0, len(wordList)-1) return wordList[wordIndex] def displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord): print(HANGMANPICS[len(missedLetters)]) print() print('Nhung chu cai doan truot:', end=' ') for letter in missedLetters: print(letter, end=' ') print() blanks = '_' * len(secretWord) for i in range(len(secretWord)): if secretWord[i] in correctLetters: blanks = blanks[:i] + secretWord[i] + blanks[i+1:] for letter in blanks: print(letter, end=' ') print() def getGuess(alreadyGuessed): # Returns the letter the player entered. This # function makes sure the player entered a single letter, and not # something else while True: print('Doan 1 chu cai') guess = input() guess = guess.lower() if len(guess) != 1: print('Chi dien vao 1 chu cai.') elif guess in alreadyGuessed: print('Chu cai do da doan roi. Doan lai.') elif guess not in 'abcdefghijklmnopqrstuvwxyz': print('Nhap 1 CHU CAI.') else: return guess def playAgain(): # This function returns True if the player wants to play again, otherwise # it returns False. print('Ban co muon choi lai khong? (yes or no)') return input().lower().startswith('y') # main code print('T R E O C O') missedLetters = '' correctLetters = '' secretWord = getRandomWord(words) gameIsDone = False while True: displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord) # Let the player type in a letter guess = getGuess(missedLetters + correctLetters) if guess in secretWord: correctLetters += guess #Check if the player has won foundAllLetters = True for i in range(len(secretWord)): if secretWord[i] not in correctLetters: foundAllLetters = False break if foundAllLetters: print('Xong! Tu bi mat do la "' + secretWord + '"! Ban da thang!') gameIsDone = True else: missedLetters = missedLetters + guess #Check if player has guesses too many times and lost if len(missedLetters) == len(HANGMANPICS) - 1: displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord) print('Da qua so lan doan!\nSau ' + str(len(missedLetters)) + ' lan doan truot va ' + str(len(correctLetters)) + ' lan doan dung, tu can doan la "' + secretWord + '"') gameIsDone = True # Ask the player if they want to play again (but only if the game is done). if gameIsDone: if playAgain(): missedLetters = '' correctLetters = '' gameIsDone = False secretWord = getRandomWord(words) else: break
Chúc các bạn vui!
File ".\DoanChu.py", line 71
Trả lờiXóaprint('Nhung chu cai doan truot:', end=' ')
^
SyntaxError: invalid syntax
bị lỗi ạ :D
chay ma ban thay dau ' bang " xem thu co bi loi gi ko nhe print("Nhung chu cai doan truot:', end=' ")
Trả lờiXóacám ơn tác giả ạ
Trả lờiXóa