Domanda

I'm trying to display 6 random integers with the following Python code:

#!"E:/PortablePython3.2.5.1/App/python.exe" 

import random

print("Content-Type: text/html\n\n")

for k in range(6):
    r = random.randint(1, 75)

print("Winning Lotto Numbers: ", r)

I can get one random integer to display, but I'm having trouble displaying the remaing 5.

È stato utile?

Soluzione

The problem is that in each iteration you are not printing anything. You are just assigning values to r. If you want to print a random integer in each iteration, then the print() call should be inside the for loop:

for k in range(6):
    r = random.randint(1, 75)
    print("Winning Lotto Numbers: ", r)

Notes:

  • You can reduce your code by printing the number directly (without assigning it to a variable):

    for k in range(6):
         print("Winning Lotto Numbers: ", random.randint(1, 75))
    

Edit:

To display "Winning Lotto Numbers: " only once, you can do:

print("Winning Lotto Numbers: ")

for k in range(6):
    print(random.randint(1, 75), end = ' ')

Altri suggerimenti

I'm guessing you need to generate a list of 6 random numbers and display them. Use a list comprehension like this:

nums = [random.randint(1, 75) for i in range(6)]
print "Winning Lotto numbers: %s" % ",".join([str(i) for i in nums])
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top