Question
In Python, write a function to allow the player to play one of two games: 1. Guess the Number: the game in chapter 3 of
In Python, write a function to allow the player to play one of two games:
1. Guess the Number: the game in chapter 3 of the book.
1. # This is a Guess the Number game. 2. import random 3. 4. guessesTaken = 0 5. 6. print('Hello! What is your name?') 7. myName = input() 8. 9. number = random.randint(1, 20) 10. print('Well, ' + myName + ', I am thinking of a number between 1 and 20.') 11. 12. for guessesTaken in range(6): 13. print('Take a guess.') # Four spaces in front of "print" 14. guess = input() 15. guess = int(guess) 16. 17. if guess < number: 18. print('Your guess is too low.') # Eight spaces in front of "print" 19. 20. if guess > number: 21. print('Your guess is too high.') 22. 23. if guess == number: 24. break 25. 26. if guess == number: 27. guessesTaken = str(guessesTaken + 1) 28. print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!') 29. 30. if guess != number: 31. number = str(number) 32. print('Nope. The number I was thinking of was ' + number + '.')
2. Cave Treasure: the game in chapter 5 of the book.
1. import random 2. import time 3. 4. def displayIntro(): 5. print('''You are in a land full of dragons. In front of you, 6. you see two caves. In one cave, the dragon is friendly 7. and will share his treasure with you. The other dragon 8. is greedy and hungry, and will eat you on sight.''') 9. print() 10. 11. def chooseCave(): 12. cave = '' 13. while cave != '1' and cave != '2': 14. print('Which cave will you go into? (1 or 2)') 15. cave = input() 16. 17. return cave 18. 19. def checkCave(chosenCave): 20. print('You approach the cave...') 21. time.sleep(2) 22. print('It is dark and spooky...') 23. time.sleep(2) 24. print('A large dragon jumps out in front of you! He opens his jaws and...') 25. print() 26. time.sleep(2) 27. 28. friendlyCave = random.randint(1, 2) 29. 30. if chosenCave == str(friendlyCave): 31. print('Gives you his treasure!') 32. else: 33. print('Gobbles you down in one bite!') 34. 35. playAgain = 'yes' 36. while playAgain == 'yes' or playAgain == 'y': 37. displayIntro() 38. caveNumber = chooseCave() 39. checkCave(caveNumber) 40. 41. print('Do you want to play again? (yes or no)') 42. playAgain = input()
you should define at least "3" functions:
1. "ChooseGame" function: asks the player to choose.
2."GuessNumber" function: game one.
3."CaveTreasure" function: game two.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started