进入宝藏洞
import time
import random
def displayFintro():
print('''You are in a land full of dragons. In front of you,
you see two caves. In one cave, the dragon is friendly
and will share his treasure with you. The other dragon
is greedy and hungry, and will eat you on sight.''')
print()
def chooseCave():
cave=''
while cave !='1' and cave !='2':
print('Which cave will you go into?( 1 or 2)')
cave=input()
return cave
def checkCave(chooseCave):
print('You approach the cave.....')
time.sleep(2)
print('It is dark and spooky....')
time.sleep(2)
print('A large dragon jumps ou in front of you! He opens his jaws and ....')
print()
time.sleep(2)
friendlyCave=random.randint(1,2)
if chooseCave == str(friendlyCave):
print('Give you his treasure!')
else:
print('Gobbles you down in one bite!')
playAgain='yes'
while playAgain=='yes' or playAgain=='Y':
displayFintro()
caveNumber =chooseCave()
checkCave(caveNumber)
print('Do you want to play again?(yes or no)')
playAgain= input()
上吊人游戏代码
import random
HANGMAN_PICS = ['''
+---+
|
|
|
===''', '''
+---+
O |
|
|
===''', '''
+---+
O |
| |
|
===''', '''
+---+
O |
/| |
|
===''', '''
+---+
O |
/|\ |
|
===''', '''
+---+
O |
/|\ |
/ |
===''', '''
+---+
O |
/|\ |
/ \ |
===''']
words = 'cat words'.split(' ')
def getRandomWord(wordList):
wordIndex = random.randint(0, len(wordList) - 1)
return wordList[wordIndex]
def displayBoard(missedLetters, correctLetters, secretword):
print(HANGMAN_PICS[len(missedLetters)])
print()
print('Missed letters:', 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):
while True:
print('Guess a letter:')
guess = input()
guess=guess.lower()
if len(guess) != 1:
print('Please enter a single letter.')
elif guess in alreadyGuessed:
print('You have already guessed that letter. Choose again.')
elif guess not in 'abcdefghijklmnopqrstuvwxyz':
print('Please enter a LETTER.')
else:
return guess
def playAgain():
print('Do you want to play again?( yes or no)')
return input().lower().startswith('y')
print('H A N G M A N')
missedLetters = ''
correctLetters = ''
secretWord = getRandomWord(words)
gameIsDone = False
while True:
displayBoard(missedLetters, correctLetters, secretWord)
guess = getGuess((missedLetters + correctLetters))
if guess in secretWord:
correctLetters = correctLetters + guess
foundAllLetters = True
for i in range(len(secretWord)):
if secretWord[i] not in correctLetters:
foundAllLetters = False
break
if foundAllLetters:
print('Yes! The secret word is ' + secretWord + '! you have won!')
gameIsDone = True
else:
missedLetters = missedLetters + guess
if len(missedLetters) == len(HANGMAN_PICS) - 1:
displayBoard(missedLetters, correctLetters, secretWord)
print('You have run out of guesses!\nAfter ' + str(len(missedLetters)) + ' missed guesses and ' + str(
len(correctLetters)) + ' correct guesses,the word was "' + secretWord + '"')
gameIsDone = True
if gameIsDone:
if playAgain():
missedLetters = ''
correctLetters = ''
gameIsDone = False
secretWord = getRandomWord(words)
else:
break