Advertisement
Guest User

Untitled

a guest
May 28th, 2015
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. #Hi/Lo Game
  2.  
  3. import random
  4.  
  5. #main takes no parameters.
  6. #It introduces the rules of the game once, then runs an while loop as long as the user wants to continue playing the game.
  7. #The while loop is where the game is played. A new random number is generated each iteration.
  8. #The for loop gives the player eight chances to guess the random number.
  9. #Each time the user guesses a number, two functions are called within the loop: one to validate the number and the other to compare it to the real answer.
  10. #If the user guesses the number correctly, or fails to guess the number after eight guesses, the game is over and the for loop is exited.
  11. def main():
  12. print("Welcome to the Hi-Lo Game.")
  13. print("The computer will generate a random number between 1 and 64 inclusive.")
  14. print("You have eight tries to guess the number before the computer wins.")
  15. print("The computer will tell you whether your guess is high, low, or correct.")
  16. while(input("Would you like to play Hi-Lo? Enter y for yes.")=="y"):
  17. myNum=random.randint(1,64)
  18. for i in range(0,9):
  19. if(i==8):
  20. print("Computer wins!")
  21. break
  22. userGuess=int(input("Your guess: "))
  23. if (inputValidation(userGuess)==False):
  24. print("Invalid input. You wasted a guess!",7-i,"guesses left.")
  25. else:
  26. if(hiLo(myNum,userGuess,i)==True):
  27. print("Correct! You win!")
  28. break
  29.  
  30. def main2():
  31. print("Welcome to the Hot-Cold Game.")
  32. print("The computer will generate a random number between 1 and 64 inclusive.")
  33. print("You have eight tries to guess the number before the computer wins.")
  34. print("The computer will tell you whether your guess is hot, cold, or correct.")
  35. while(input("Would you like to play Hot-Cold? Enter y for yes.")=="y"):
  36. myNum=random.randint(1,64)
  37. for i in range(0,9):
  38. if(i==8):
  39. print("Computer wins!")
  40. break
  41. userGuess=int(input("Your guess: "))
  42. if (inputValidation(userGuess)==False):
  43. print("Invalid input. You wasted a guess!",7-i,"guesses left.")
  44. else:
  45. if(hotCold(myNum,userGuess,i)==True):
  46. print("Correct! You win!")
  47. break
  48.  
  49. #inputValidation takes one parameter, a number that the user enters.
  50. #It checks to see if the user entered a number between 1 and 64, and returns a boolean value (True if the user input is valid, False otherwise)
  51. def inputValidation(a):
  52. if(1<=a and a<=64):
  53. return True
  54. else:
  55. return False
  56.  
  57. #hiLo takes three parameters: the computer-generated random int, the number that the user enters, and the c of the definite loop in main
  58. #it checks if the user has guessed high, low, or correctly, and returns a boolean value (True if the user guess is correct, False otherwise)
  59. def hiLo(a,b,c):
  60. if(a==b):
  61. return True
  62. elif(a>b):
  63. print("Too low.",7-c,"guesses left")
  64. return False
  65. else:
  66. print("Too high.",7-c,"guesses left")
  67. return False
  68.  
  69. def hotCold(a,b,c):
  70. if(a==b):
  71. return True
  72. elif(32<=a-b or a-b<=-32):
  73. print("Freezing.",7-c,"guesses left")
  74. return False
  75. elif(16<=a-b or a-b<=-16):
  76. print("Cold.",7-c,"guesses left")
  77. return False
  78. elif(8<=a-b or a-b<=-8):
  79. print('Warm.',7-c,'guesses left')
  80. return False
  81. elif(4<=a-b or a-b<=-4):
  82. print('Hot!',7-c,'guesses left')
  83. return False
  84. elif(2<=a-b or a-b<=-2):
  85. print('Very hot!',7-c,'guesses left')
  86. return False
  87. else:
  88. print('BOILING HOT!',7-c,'guesses left')
  89. return False
  90.  
  91. main()
  92. main2()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement