Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Create import of random number
- import random
- # Define function for a guessing game
- def guessing_game():
- again = 'y'
- while again != 'n':
- random_num = random.randint (1, 100)
- print("Let's play a number guessing game!")
- guess = int(input('\nGuess a number between 1 and 100: '))
- guess_count = int(0)#this too
- repeat = True #Inital start
- while repeat != False: #control loop for guesses
- if guess > random_num:
- guess = int(input('\nToo high! Guess again!: '))
- elif guess < random_num:
- guess = int(input('\nToo low! Guess again!: '))
- else:
- repeat = False #if player guesses correctly, it will proceed
- guess_count +=1
- if guess_count == 1:
- print('Congratulations! You guessed the correct number in', guess_count, 'attempt!')
- else:
- print('You guessed the correct number in', guess_count, 'attempts!')
- # Prompt user to play again or quit
- keep_playing = str(input('Would you like to play again?\n' \
- '(Yes/No): ').lower())
- if keep_playing != 'Yes':
- again = False
- print('Thanks for playing! Goodbye!')
- guessing_game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement