Python Challenge #2: Guessing The Number 12-16-2015, 08:04 PM
#1
This is usually a good beginner challenge. And maybe for m0dem to write it in half the lines I do.
Create a program that lets the user guess a random number. If the guess is too high or too low, tell them. The game should keep track of the number of wrong answers and display how many guesses it took to complete. The user should also have the option of replaying the game after winning.
*ADVANCED VERSION*
Make it so you set the number and the computer needs to guess it.
My reg version:
Advanced Version:
When you're boss af:
Create a program that lets the user guess a random number. If the guess is too high or too low, tell them. The game should keep track of the number of wrong answers and display how many guesses it took to complete. The user should also have the option of replaying the game after winning.
*ADVANCED VERSION*
Make it so you set the number and the computer needs to guess it.
My reg version:
Spoiler:
Code:
import random
def guess_check():
random_number = random.randint(1, 100)
guesses = 0
guess = int(input('Guess the number: '))
while guess != random_number:
guesses+=1
if guess > random_number:
print('Too high, try again. ')
guess = int(input('Guess the number: '))
elif guess < random_number:
print('Too low, try again. ')
guess = int(input('Guess the number: '))
if guess == random_number:
guesses+=1
print('Congratulations! You guessed the number in',guesses, 'guesses.')
def start_over():
play_again = input('Do you want to play again? (Y or N) ')
while play_again.lower() == 'y':
guess_check()
guess_check()
start_over()Advanced Version:
Spoiler:
Code:
import random
correct_number = int(input('Enter a number for the computer to guess (1 - 100): '))
guesses = 0
def computer_guess(correct_number, guesses):
comp_guess = random.randint(1, 100)
guesses += 1
print('The computer guessed', comp_guess)
while correct_number > comp_guess:
print('Too low!')
comp_guess = random.randint(comp_guess +1, 100)
print('The computer guessed', comp_guess)
guesses += 1
while correct_number < comp_guess:
print('Too high!')
comp_guess = random.randint(1, comp_guess - 1)
print('The computer guessed', comp_guess)
guesses += 1
else:
print('The computer guessed the number in', guesses, 'attempts!')
computer_guess(correct_number, guesses)When you're boss af:





![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)
















