0

for my lottery i have a jackpot prize randomly chosen from 100,000 after its randomly chosen it goes to numbers how would i put it to be a currency? here is my jackpot code.

prize=random.randrange(100000)

print "welcome to the lottery!!!!"
time.sleep(1)
name=raw_input("What is your name?")
print name
print "welcome to the game show where you can win thousands of pounds by just ghuesing a number!!"
number=random.randrange(100)
while True:
    ghuess=input("state a number between 1-100")
    if ghuess>number:
        print "too high try again!"
    elif ghuess<number:
        print "too low try again!"
    else:
        # Jackpot, exit the loop.
        break
print "well done! ghuess you have won.."
time.sleep(1)
print "3"
time.sleep(1)
print "2"
time.sleep(1)
print "1"
time.sleep(1)
print prize
atlasologist
  • 3,824
  • 1
  • 21
  • 35
Tristan
  • 7
  • 1
  • 3
  • 4
    By "put it to be currency" do you mean print it with two decimals, commas between the thousands, etc? If so, please see here https://stackoverflow.com/questions/320929/currency-formatting-in-python?rq=1 – Cory Kramer Apr 03 '14 at 11:53
  • You can see this [post](http://stackoverflow.com/a/320951/1982962) or this [post](http://stackoverflow.com/a/8851191/1982962) it will solve the problem. – Kobi K Apr 03 '14 at 11:58

3 Answers3

2

You can easily do that with string formatting as follows:

>>> print "${:,.2f}".format(prize)

Example:

>>> prize = 12345678   #just an example
>>> print "${:,.2f}".format(prize)
$12,345,678.00

Hope that helps.

sshashank124
  • 31,495
  • 9
  • 67
  • 76
2

If all you do is print the prize:

In [77]: print "{:,.2f}£".format(random.randrange(100000))
26,467.00£

Or if you want the currency symbol in the front

In [78]: print "£{:,.2f}".format(random.randrange(100000))
£80,244.00

If you already have the prize variable:

In [80]: print "£{:,.2f}".format(prize)
£64,058.00

Here's a good explanation of the format specification.

msvalkon
  • 11,887
  • 2
  • 42
  • 38
0

Just multiple the random value by 0.01. Like below:

prize=random.randrange(100000) * 0.01
Zini
  • 909
  • 7
  • 15
  • That still lacks the currency symbol (if that should be dynamic) as well as the comma separation every three digits. – Cory Kramer Apr 03 '14 at 11:56