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
有帮助吗?

解决方案

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.

其他提示

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.

Just multiple the random value by 0.01. Like below:

prize=random.randrange(100000) * 0.01
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top