Reputation: 147
So I need to make a program that simulates the Powerball.
import random
def powerball():
randInt = 0
first_bin = [1]
for i in range(1,70):
first_bin.append(i)
#Create second bin - a list of integers from 1-26
sec_bin = [1]
for i in range(1,27):
sec_bin.append(i)
#Mix up the bins - .shuffle()
random.shuffle(first_bin)
random.shuffle(sec_bin)
#Select 5 numbers from bin 1
#for loop
#.pop() to pull a ball
#.randInt to pick a position in the list
for i in range(65):
first_bin.pop(randInt)
#Sort our list of 5 numbers
first_bin.sort
#Pick the Powerball from bin 2 - .choice() & append that to our list of 5 numbers
ball = random.choice(sec_bin)
power = first_bin.append(ball)
#return our list
return power
#main program - call the function and print the result
print(powerball())
Whenever I try to run the program, it prints "function powerball at 0x7f10b46283a0". It only started doing this after the last couple of lines. What's going wrong? I checked and it seems like it would run fine.
Upvotes: 1
Views: 217
Reputation: 2277
that's because you do append and you assign that to the variable.
try this code:
import random
def powerball():
randInt = 0
first_bin = [1]
for i in range(1,70):
first_bin.append(i)
#Create second bin - a list of integers from 1-26
sec_bin = [1]
for i in range(1,27):
sec_bin.append(i)
#Mix up the bins - .shuffle()
random.shuffle(first_bin)
random.shuffle(sec_bin)
#Select 5 numbers from bin 1
#for loop
#.pop() to pull a ball
#.randInt to pick a position in the list
for i in range(65):
first_bin.pop(randInt)
#Sort our list of 5 numbers
first_bin.sort() # fixed this sort -> sort()
#Pick the Powerball from bin 2 - .choice() & append that to our list of 5 numbers
ball = random.choice(sec_bin)
first_bin.append(ball) #added this
power = first_bin #changed this
#return our list
return power
#main program - call the function and print the result
print(powerball())
I fixed power = first_bin.append(ball)
this is wrong. append
just appends something to the list and it returns nothing(none
). so just call it first and set power to first_bin
like this: power = first_bin
use print(powerball())
instead of print(powerball)
you have to use sort()
instead of sort
Upvotes: 1
Reputation: 191831
The first issue is that you need to call the function, as mentioned.
The second is that append
will modify the list, and always returns None, which you then return yourself. So, while the idea might be correct, you don't need another variable for it
For example
import random
def powerball():
first_bin = list(range(1, 70))
random.shuffle(first_bin)
result = sorted(first_bin[:5])
ball = random.randint(1, 27)
result.append(ball)
return result
print(powerball())
Upvotes: 1