#Guess the number game by Johnathan Millsap
import random

guessesTaken = 0

print('Hello! What is your name?')
myName = input()

number = random.randint(1, 100)
print('Well, ' + myName + ', I am thinking of a number between 1 and 100.')
print('Try to guess it!.')

while guessesTaken <= 3:
    print('Take a guess.')
    guesesTaken = guessesTaken + 1
    guess = input()
    guess = int(guess)

    if guess < number:
        print('Your guess is too low.')     
    if guess > number:
        print('Your guess is too high.')
    if guess == number:
        break

if guess == number:
    guesesTaken = str(guessesTaken)
    print('Good job, ' + myName + '! You guessed my number ' + str(guessesTaken) + ' guesses!')

if guess != number:
    number = str(number)
    print('Nope. The number I was thinking of was ' + number)

I have the basis of the program, but I need to add more parameters like <=3 but instead >3 and <=7 and another that is just >7. How do I add this into my existing code? And my last question is how can I add in the choice for difficulty? Like if I type in 1 when prompted it will set the game parameters to 1-100 and if I type 2 it makes it 1-1000 and so forth?

Recommended Answers

All 3 Replies

Well, I know c++ more than python. But since no one has replied yet I'll take a shot at this.
There are several ways to go about adding difficulty, I would do something like having a menu in the beginning asking for the difficulty, say 1-3, 1 is easy and so on. Then having the variable that stores the difficulty level trigger then range of numbers.

ex(psuedolanguage):

 'difficulty?'
    //user input = 2
    if input == 1
    "max = 100"
    if input == 2
    "max = 1000"
    ect.. 
number = random.randint(1, max)
print('Well, ' + myName + ', I am thinking of a number between 1 and '+max+'.')
print('Try to guess it!.')

Anymore questions feel free to ask here or PM me :3
Maybe I can help :D

You could do something like this ...

# Guess the number game by Johnathan Millsap

import random

myName = input('Hello! What is your name? ')
level = int(input('Pick a level of difficulty (1 to 3): '))
# change range and number of allowed guesses with level
if level == 1:
    top = 100
    tries = 3
elif level == 2:
    top = 500
    tries = 6
else:
    top = 1000
    tries = 10
# create a '%' formatted string
sf = "Well, %s, I am thinking of a number between 1 and %d" 
print(sf % (myName, top))

# pick the random integer
number = random.randint(1, top)

print('Try to guess it!.')

guessesTaken = 0
while guessesTaken <= tries:
    guess = int(input('Take a guess: '))
    guessesTaken += 1
    if guess < number:
        print('Your guess is too low.')     
    if guess > number:
        print('Your guess is too high.')
    if guess == number:
        break

if guess == number:
    # create a '%' formatted string
    sf = "Good job, %s! You guessed my number with %d guesses!"
    print(sf % (myName, guessesTaken))

if guess != number:
    number = str(number)
    print('Nope. The number I was thinking of was ' + number)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.