8 Replies - 1216 Views - Last Post: 26 January 2013 - 02:48 PM Rate Topic: -----

#1 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 63
  • Joined: 07-October 12

Python 3.3.0 storing new random values

Posted 23 January 2013 - 04:54 AM

I was wondering how to store a new random value, since I am trying to make a guessing game. Where user guesses a number. I created a while loop where the loop will only break if the user gets number correct. But the problem is that when I generate a random number, it will store the same value until you guessed correct.

Output:
Guess a number: 28
Sorry the number was: 29

And it will keep doing that until, you guessed the right number. Therefore my question is how to update the random number, every time when the person guesses the right number?

import random
random_number, lucky_number = random.randint(0,100), 0

lucky_number = int(input("Guess a number:"))

if lucky_number != random_number:
    while random_number != lucky_number:
            lucky_number = int(input("Guess a number:"))
            print("The number was %d." %(random_number))

elif random_number == lucky_number:
    print("You have guessed the right number!")
    break


Is This A Good Question/Topic? 0
  • +

Replies To: Python 3.3.0 storing new random values

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: Python 3.3.0 storing new random values

Posted 23 January 2013 - 06:15 AM

Your indentation is wrong. And what is the point of printing the random number when the user did not guess correctly. Give them some chance of getting it right. And

print("The number was %d" % (random))


will not work. You need to use format() as in:
print("THe number was {0}".format(random_number))

Was This Post Helpful? 0
  • +
  • -

#3 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Python 3.3.0 storing new random values

Posted 23 January 2013 - 06:17 AM

Populate the random number variable before your while loop, and then set it again every time it is correctly guessed:

random_number = get_random_number()

while True:
    chosen_number = get_user_guess()
    if chosen_number == lucky_number:
        print "You win"
        random_number = get_random_number()
    else:
        print "Try again"


Was This Post Helpful? 1
  • +
  • -

#4 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 63
  • Joined: 07-October 12

Re: Python 3.3.0 storing new random values

Posted 23 January 2013 - 07:58 AM

View Postdarek9576, on 23 January 2013 - 06:15 AM, said:

Your indentation is wrong. And what is the point of printing the random number when the user did not guess correctly. Give them some chance of getting it right. And

print("The number was %d" % (random))


will not work. You need to use format() as in:
print("THe number was {0}".format(random_number))


I believe we are not using the same Python, make sure to read the title that I am currently using Python 3.3.x, there may be some difference in the Python that you are currently using. String formatting is very common to be used in Python, makes your program more efficient.

I did some research it is called: String Formatting Operations
More information about string formatting operation

Edit:
User has one try, that what people have told me to do... I was going to give three chances by adding arguments giving hints if they are close to the number or farther from the number, although seems that the teacher wants the user to enter one time, and display the random number that have been generated.

This post has been edited by chan 06: 23 January 2013 - 08:00 AM

Was This Post Helpful? 1
  • +
  • -

#5 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: Python 3.3.0 storing new random values

Posted 23 January 2013 - 08:03 AM

It does work. Weird.
Was This Post Helpful? 0
  • +
  • -

#6 atraub  Icon User is offline

  • Pythoneer
  • member icon

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

Re: Python 3.3.0 storing new random values

Posted 26 January 2013 - 01:58 PM

While it % formatting string is usable, it's also deprecated, so best practices dictate you shouldn't use it.
Was This Post Helpful? 1
  • +
  • -

#7 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: Python 3.3.0 storing new random values

Posted 26 January 2013 - 02:25 PM

I always thought % for string formatting would not work in Python 3.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3458
  • View blog
  • Posts: 11,707
  • Joined: 12-December 12

Re: Python 3.3.0 storing new random values

Posted 26 January 2013 - 02:43 PM

View Postdarek9576, on 26 January 2013 - 02:25 PM, said:

I always thought % for string formatting would not work in Python 3.


String interpolation is available across both Python 2.* and Python 3.*. I find it particularly useful with HEREDOCs (for HTML templating) so hope that it remains!

This post has been edited by andrewsw: 26 January 2013 - 02:44 PM

Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3458
  • View blog
  • Posts: 11,707
  • Joined: 12-December 12

Re: Python 3.3.0 storing new random values

Posted 26 January 2013 - 02:48 PM

We can also use a dictionary for string interpolation, which is really cool :)

CSS_MAIN = \
"""
    <style type="text/css">
    body { color: %(fcolor)s; background-color: %(bcolor)s;
        font: %(fsize)dpt '%(fface)s', Consolas, Monospace; }
    #preCode { border: 0; margin: 0; margin-top: 1em; padding: 0;
        font: %(fsize)dpt '%(fface)s', Consolas, Monospace; }
    span { color: %(fcolor)s; background-color: %(bcolor)s;
        display: inline; border: 0; margin: 0; padding: 0; }
    * html a:hover { background: transparent; }
"""

the_html.write((CSS_MAIN % {"fcolor": self.fground, "bcolor": self.bground, \
            "fsize": self.font_size, "fface": self.font_face}).encode(*UTF8))

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1