8 Replies - 6037 Views - Last Post: 17 July 2012 - 08:49 AM Rate Topic: -----

#1 cladus  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 11-June 12

Guess the Number Game - Example Code -

Posted 12 June 2012 - 05:27 AM

I have recently been learning python code so I decided to do a Guess the Number Game.
Note: Please do not publish code under your name, Thank You :)
This is the Code:
#Random Number Guess
#You get as many tries at guessing
#the pesudorandom number, as you want!
#Novus Cladus 11/06/2012
import random
print "Random Number Guess"
print "\n"
number = random.randrange(100)+1
loopnumber = 0
tries = int(raw_input("How many tries would you like? "))
frozentries = tries
while loopnumber != frozentries:
    loopnumber += 1
    guess = int(raw_input("Please enter your " + str(loopnumber) + " Guess: "))
    if guess == number:
        print("You Won!!!!")
        break
    if loopnumber == frozentries:
        print("Game Over")
        print("Answer: " + str(number))
        break
    if guess > number:
        print("Too High")
    if guess < number:
        print("Too Low")


I hope you like it :)

I found some mistakes in my code so I modified it. Here's the new code:
#Random Number Guess
#You get as many tries at guessing
#the pesudorandom number, as you want!
#Novus Cladus 11/06/2012
import random
print "Random Number Guess"
print "\n"
number = random.randrange(100)+1
loopnumber = 0
tries = int(raw_input("How many tries would you like? "))
while loopnumber != tries:
    loopnumber += 1
    guess = int(raw_input("Please enter your " + str(loopnumber) + " Guess: "))
    if guess == number:
        print("You Won!!!!")
        break
    if loopnumber == tries:
        print("Game Over")
        print("Answer: " + str(number))
        break
    if guess > number:
        print("Too High")
    if guess < number:
        print("Too Low")



Is This A Good Question/Topic? 1
  • +

Replies To: Guess the Number Game - Example Code -

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Guess the Number Game - Example Code -

Posted 13 June 2012 - 10:33 AM

I promise, no one here will publish your code under their name.
Was This Post Helpful? 2
  • +
  • -

#3 cladus  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 11-June 12

Re: Guess the Number Game - Example Code -

Posted 13 June 2012 - 11:01 AM

Haha :) I don't know why I put that there.
Was This Post Helpful? 0
  • +
  • -

#4 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: Guess the Number Game - Example Code -

Posted 12 July 2012 - 06:42 PM

View Postcladus, on 13 June 2012 - 01:01 PM, said:

Haha :) I don't know why I put that there.

Good work man; thanks to you, I was inspired to make an attempt at this guessing game. (I don't think its 100% functional but I learned about random() from you. Thanks
def main():
    while True:
        request = str(input('Play? [y/N]: '))
        if 'y' in request.lower():
            phase2()
        elif 'n' in request.lower():
            break
        else:
            print('Invalid Value')
            main()

def phase2():
    import random
    answer = random.randrange(0, 101)
    start = 0
    while (start < 6):
        print ('You only have 5 chances (number is in between 0 - 100)')
        print ('Attempt', start) 
        retry = int(input('Guess:> '))
        start += 1
        if retry < answer:
            print ('A little higher')
        elif retry > answer:
            print ('A little lower')
        elif retry == answer:
            print ('Congrats')
        else:
            print ('Invalid Input')
        
main()


I plan on messing with this more, but for now this is what I made
Was This Post Helpful? 0
  • +
  • -

#5 CJ Siege  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-February 10

Re: Guess the Number Game - Example Code -

Posted 16 July 2012 - 01:36 PM

View Postk3y, on 12 July 2012 - 09:42 PM, said:

View Postcladus, on 13 June 2012 - 01:01 PM, said:

Haha :) I don't know why I put that there.

Good work man; thanks to you, I was inspired to make an attempt at this guessing game. (I don't think its 100% functional but I learned about random() from you. Thanks
def main():
    while True:
        request = str(input('Play? [y/N]: '))
        if 'y' in request.lower():
            phase2()
        elif 'n' in request.lower():
            break
        else:
            print('Invalid Value')
            main()

def phase2():
    import random
    answer = random.randrange(0, 101)
    start = 0
    while (start < 6):
        print ('You only have 5 chances (number is in between 0 - 100)')
        print ('Attempt', start) 
        retry = int(input('Guess:> '))
        start += 1
        if retry < answer:
            print ('A little higher')
        elif retry > answer:
            print ('A little lower')
        elif retry == answer:
            print ('Congrats')
        else:
            print ('Invalid Input')
        
main()


I plan on messing with this more, but for now this is what I made


Looking at this code, what does random.randrange(x,y) do that random.randint(x,y) doesn't? Are they interchangeable or do they return something different?
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7948
  • View blog
  • Posts: 13,535
  • Joined: 19-March 11

Re: Guess the Number Game - Example Code -

Posted 16 July 2012 - 01:40 PM

Hello, documentation!

Quote

random.randrange([start], stop[, step])
Return a randomly selected element from range(start, stop, step). This is equivalent to choice(range(start, stop, step)), but doesn’t actually build a range object.


random.randint(a, B)
Return a random integer N such that a <= N <= b.


Looks like randrange allows you to specify a step, as ranges do. So you could presumably get a random even number between 2 and 48, or something.

Try it and report back.
Was This Post Helpful? 1
  • +
  • -

#7 CJ Siege  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-February 10

Re: Guess the Number Game - Example Code -

Posted 16 July 2012 - 02:35 PM

View Postjon.kiparsky, on 16 July 2012 - 04:40 PM, said:

Hello, documentation!

Quote

random.randrange([start], stop[, step])
Return a randomly selected element from range(start, stop, step). This is equivalent to choice(range(start, stop, step)), but doesn’t actually build a range object.


random.randint(a, B)
Return a random integer N such that a <= N <= b.


Looks like randrange allows you to specify a step, as ranges do. So you could presumably get a random even number between 2 and 48, or something.

Try it and report back.


Thank you! So, in this instance, it looks like random.randint would have worked equally well since integers were specified and a step was not. Right?
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7948
  • View blog
  • Posts: 13,535
  • Joined: 19-March 11

Re: Guess the Number Game - Example Code -

Posted 16 July 2012 - 03:03 PM

Let's find out....

The interpreter says yes:
>>> import random
>>> random.randrange(1,10,1)
8
>>> random.randrange(1,10)
2
>>> random.randint(1,10)
7
>>> 


This post has been edited by jon.kiparsky: 16 July 2012 - 03:04 PM

Was This Post Helpful? 0
  • +
  • -

#9 cladus  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 11-June 12

Re: Guess the Number Game - Example Code -

Posted 17 July 2012 - 08:49 AM

I'm glad to have helped your code :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1