Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Hi/Lo Game
- import random
- #main takes no parameters.
- #It introduces the rules of the game once, then runs an while loop as long as the user wants to continue playing the game.
- #The while loop is where the game is played. A new random number is generated each iteration.
- #The for loop gives the player eight chances to guess the random number.
- #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.
- #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.
- def main():
- print("Welcome to the Hi-Lo Game.")
- print("The computer will generate a random number between 1 and 64 inclusive.")
- print("You have eight tries to guess the number before the computer wins.")
- print("The computer will tell you whether your guess is high, low, or correct.")
- while(input("Would you like to play Hi-Lo? Enter y for yes.")=="y"):
- myNum=random.randint(1,64)
- for i in range(0,9):
- if(i==8):
- print("Computer wins!")
- break
- userGuess=int(input("Your guess: "))
- if (inputValidation(userGuess)==False):
- print("Invalid input. You wasted a guess!",7-i,"guesses left.")
- else:
- if(hiLo(myNum,userGuess,i)==True):
- print("Correct! You win!")
- break
- def main2():
- print("Welcome to the Hot-Cold Game.")
- print("The computer will generate a random number between 1 and 64 inclusive.")
- print("You have eight tries to guess the number before the computer wins.")
- print("The computer will tell you whether your guess is hot, cold, or correct.")
- while(input("Would you like to play Hot-Cold? Enter y for yes.")=="y"):
- myNum=random.randint(1,64)
- for i in range(0,9):
- if(i==8):
- print("Computer wins!")
- break
- userGuess=int(input("Your guess: "))
- if (inputValidation(userGuess)==False):
- print("Invalid input. You wasted a guess!",7-i,"guesses left.")
- else:
- if(hotCold(myNum,userGuess,i)==True):
- print("Correct! You win!")
- break
- #inputValidation takes one parameter, a number that the user enters.
- #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)
- def inputValidation(a):
- if(1<=a and a<=64):
- return True
- else:
- return False
- #hiLo takes three parameters: the computer-generated random int, the number that the user enters, and the c of the definite loop in main
- #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)
- def hiLo(a,b,c):
- if(a==b):
- return True
- elif(a>b):
- print("Too low.",7-c,"guesses left")
- return False
- else:
- print("Too high.",7-c,"guesses left")
- return False
- def hotCold(a,b,c):
- if(a==b):
- return True
- elif(32<=a-b or a-b<=-32):
- print("Freezing.",7-c,"guesses left")
- return False
- elif(16<=a-b or a-b<=-16):
- print("Cold.",7-c,"guesses left")
- return False
- elif(8<=a-b or a-b<=-8):
- print('Warm.',7-c,'guesses left')
- return False
- elif(4<=a-b or a-b<=-4):
- print('Hot!',7-c,'guesses left')
- return False
- elif(2<=a-b or a-b<=-2):
- print('Very hot!',7-c,'guesses left')
- return False
- else:
- print('BOILING HOT!',7-c,'guesses left')
- return False
- main()
- main2()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement