Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
July 22nd, 2012, 01:42 PM
 bloodycage
Registered User

Join Date: Jul 2012
Posts: 5
Time spent in forums: 1 h 3 m 24 sec
Reputation Power: 0
Help with python guess the number

Hi, I'm am very very new to programming and python is my first coding language, Ive read tutorials online and they showed me how to make a guess the number game, so i did, and it worked fine, but i tried to make my own with different difficulty settings, and on easy it say 1-10 and when you guess 7 its too high, but 6 is too low, Ive set an if number == guess, and Ive changed the input to an integer its just not matching up :/

Code:
```import random
guesses = 0
print ('welcome to the guess the number game, whats your name?')
name = input()
print()
print('nice to meet you ' + name + ', please select your difficulty level:')
print()
print('easy')
print('normal')
print('hard')
difficulty = input()
while difficulty == 'easy':
while guesses < 7:
print ('Im thinking of a number between 1 and 20')
number = random.randint(2, 19)
print('take a guess...')
guess = int(input())
guesses = guesses + 1
if guess < number:
print('that number is too low')
if guess > number:
print('that number is too high')
if guess == number:
difficulty = 4 #i tried to set it as none first but i got an error,
break          #4 is just a random thing i threw in there to cut off
while difficulty == 'normal':#easy, normal, and hard
while guesses < 6:
print ('Im thinking of a number between 1 and 25')
number = random.randint(2, 24)
print('take a guess...')
guess = int(input())
guesses = guesses + 1
if guess < number:
print('that number is too low')
if guess > number:
print('that number is too high')
if guess == number:
difficulty = 4
break
while difficulty == 'hard':
while guesses < 5:
print ('Im thinking of a number between 1 and 30')
number = random.randint(2, 29)
print('take a guess...')
guess = int(input())
guesses = guesses + 1
if guess < number:
print('that number is too low')
if guess > number:
print('that number is too high')
if guess == number:
difficulty = 4
break
if guess == number:
guesses = str(guessesTaken)
print('Good job, ' + name + '! You guessed my number in ' + guesses + ' guesses!')
if guess != number:
number = str(number)
print('Nope. The number I was thinking of was ' + number)```

#2
July 22nd, 2012, 08:39 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,132
Time spent in forums: 1 Month 3 Weeks 2 Days 5 h 47 m 38 sec
Reputation Power: 455

I played your game once at the easy level. It worked correctly until I got to the undefined `guessesTaken' variable which should be `guesses'. Again, please, what is the trouble?
__________________
[code]Code tags[/code] are essential for python code!

#3
July 22nd, 2012, 09:07 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,132
Time spent in forums: 1 Month 3 Weeks 2 Days 5 h 47 m 38 sec
Reputation Power: 455
oops I didn't mean to push the submit button yet.

You have the comment
#i tried to set it as none first but i got an error,

None is spelled with a capital N as in None and that could be the trouble.

And, oops, your game only worked by luck. The problem is is that you keep changing the target number.
number=random.randint(2,maximum)
needs to be outside the innermost while loop.
See rewrite. You have a lot of duplicate code. Soon you'll learn to write functions. You'll also learn about the else and elif clauses on an if statement.
Code:
```import random

def game(maximum,limit):
guesses = limit
number = random.randint(2, maximum-1) # CHOOSE THE NUMBER HERE, JUST ONE TIME
message = "I'm thinking of a number between 1 and {}".format(maximum)
while guesses:
print(message)
print('take a guess...')
guess = int(input())
guesses -= 1
if guess < number:
print('that number is too low')
elif guess > number:
print('that number is too high')
else: # they must be equal
return (limit-guesses,number,)
return (0,number,)

print ('welcome to the guess the number game, whats your name?')
name = input()
print()
print('nice to meet you ' + name + ', please select your difficulty level:')
print()
print('easy')
print('normal')
print('hard')
difficulty = input()
difficulty_arguments = dict(easy=(20,7),normal=(25,6),hard=(30,5))
try:
arguments = difficulty_arguments[difficulty]
except KeyError:
arguments = difficulty_arguments['normal']

(guesses_taken,number,) = game(*arguments)

if guesses_taken:
print('Good job, {}! You guessed my number in {} guesses!'.format(name,guesses_taken))
else:
print('Nope. The number I was thinking of was {}'.format(number))```
bloodycage agrees!

Last edited by b49P23TIvg : July 22nd, 2012 at 09:12 PM.

#4
July 22nd, 2012, 10:32 PM
 bloodycage
Registered User

Join Date: Jul 2012
Posts: 5
Time spent in forums: 1 h 3 m 24 sec
Reputation Power: 0
Thanks

i understand now, thank you so much and sorry for the trouble i was just stumped, Lol, i understand the how the new code works in general, theres just a couple things i didn't get, but I'm sure ill get to it very soon, thanks again

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Help with python guess the number