We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,852 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Guessing Generator with difficulty levels

#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?

3
Contributors
3
Replies
2 Hours
Discussion Span
5 Months Ago
Last Updated
4
Views
Question
Answered
johnathan.millsap
Newbie Poster
1 post since Dec 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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!.')
Prysm[VA]
Newbie Poster
21 posts since Dec 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

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

Prysm[VA]
Newbie Poster
21 posts since Dec 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

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)
vegaseat
DaniWeb's Hypocrite
Moderator
6,475 posts since Oct 2004
Reputation Points: 1,447
Solved Threads: 1,611
Skill Endorsements: 36
Question Answered as of 5 Months Ago by Prysm[VA] and vegaseat

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0674 seconds using 2.71MB