Advertisement
Guest User

Untitled

a guest
May 28th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. import random
  2.  
  3. #main takes no parameters.
  4. #It introduces the rules of the game once, then runs an while loop as long as the user wants to continue playing the game.
  5. #The while loop is where the game is played. A new random number is generated each iteration.
  6. #The for loop gives the player eight chances to guess the random number.
  7. #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.
  8. #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.
  9. def main():
  10. print("Welcome to the Hi-Lo Game.")
  11. print("The computer will generate a random number between 1 and 64 inclusive.")
  12. print("You have eight tries to guess the number before the computer wins.")
  13. print("The computer will tell you whether your guess is high, low, or correct.")
  14. while(input("Would you like to play Hi-Lo? Enter y for yes.")=="y"):
  15. myNum=random.randint(1,64)
  16. for i in range(0,9):
  17. if(i==8):
  18. print("Computer wins!")
  19. break
  20. userGuess=int(input("Your guess: "))
  21. if (inputValidation(userGuess)==False):
  22. print("Invalid input. You wasted a guess!",7-i,"guesses left.")
  23. else:
  24. if(hiLo(myNum,userGuess,i)==True):
  25. print("Correct! You win!")
  26. break
  27.  
  28.  
  29. #inputValidation takes one parameter, a number that the user enters.
  30. #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)
  31. def inputValidation(a):
  32. if(1<=a and a<=64):
  33. return True
  34. else:
  35. return False
  36.  
  37. #hiLo takes three parameters: the computer-generated random int, the number that the user enters, and the c of the definite loop in main
  38. #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)
  39. def hiLo(a,b,c):
  40. if(a==b):
  41. return True
  42. elif(a>b):
  43. print("Too low.",7-c,"guesses left")
  44. return False
  45. else:
  46. print("Too high.",7-c,"guesses left")
  47. return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement