To win the Powerball lottery (an extremely unlikely event so don't waste your time) you have to pick six numbers correctly. The first five numbers are drawn from a drum containing 53 balls and the sixth is drawn from a drum containing 42 balls. The chances of doing this are 1 in 120,526,770.
The output needs to be in the form:
Official (but fruitless) Powerball number generator
How many sets of numbers? 3
Your numbers: 3 12 14 26 47 Powerball: 2
Your numbers: 1 4 31 34 51 Powerball: 17
Your numbers: 10 12 49 50 53 Powerball: 35
import random
#Powerball
print "Offical Powerball number generaor"
x = int(raw_input("How many sets of numbers? "))
z = range(1,42)
z1 = random.choice(z)
def list1():
l1=[]
n=1
while n<=5:
y = range(1,53)
y1 = random.choice(y)
l1.append(y1)
n +=1
print sorted(l1)
i=1
while i<=x:
# print "Your numbers: " + list1() + "Powerball: "+ str(z1)
print list1()
raw_input("Press<enter>")
My code's output goes on a infinite loop. I have to kill it. And the message is:
None
[2, 7, 22, 33, 42]
None
[15, 19, 19, 26, 48]
None
[1, 5, 7, 26, 41]
None
[7, 42, 42, 42, 51]
None
..... etc ....
while i<=x: - you never increment i, so it is stuck in your last loop...
To avoid such things and remove the noise of i+=1 lines in your code I suggest using for loops for i in range(x) and for n in range(5).
Better yet, the following expression can replace list1:
[random.choice(range(1,53)) for x in xrange(5)]
At least, that does the same as your code. But what you probably really want (to avoid the same ball being chosen twice) is:
random.sample( range(1,53), 5 )
Related
Input: list = [23, 23, 25, 34, 34, 32, 34]
Here 34 occurs 3 times, whereas 23 occurs 2 times.
I need to print the second lowest value. In this case output should be 23
I'm not able to figure out how I can pass the list as an argument and get the required output.
I don't even know how to format this paragraph right haha
Hi there I'm completely new to Python.
I was wondering if I have a list
how do I take the output of a first if command and use on the next
Example
L= [23, 91, 0, -11, 4, 23, 49]
for i in L:
if(i > 10):
print(i * 30)
example
I also want to check if the output of this number is an even number if so (+6) -- for the output of that
if number is not equal to -11 I want to add 10.
So I got 23 > 10= (23*30 = 690) // 690 is an even number ( 690 + 6=696) // it not equal to -11 so (696+10= 706).
How do I do this for every number on the list? Excuse my lack of knowledge it's literally my first python class and my first exercises.
Is that what you want?
L= [23, 91, 0, -11, 4, 23, 49]
for i in L:
if i > 10:
num = i * 30
if num % 2 == 0:
num += 6
if i != -11:
num += 10
else:
pass
print(num)
output:
706
2746
706
1486
The objective of this code is to print sum of multiples of 3 and 5 between given range. In hackerrank, I am getting error in test case 2 and 3. Anyone knows how to resolve the issue?
t =int(raw_input())
for i in range(0,t):
range = int(raw_input())
a=3
b=5
aa=[]
res=[]
def forA(a): #Calculating Multiple of 3
while True:
if a >=range :
a = a-3
break
else:
aa.append(a)
a += 3;
def forB(b): #Calculating Multiple of 5
while True:
if b >=range :
b=b-5
return b
break
else:
aa.append(b)
b += 5;
forA(a)
forB(b)
for i in aa: #eliminate duplicate values of multiples.
if i not in res:
res.append(i)
print sum(res)
This might resolve your problems because it uses pythons builtin features that are "smart" instead of creating a list one item a time yourself. Therefore they are faster... which might solve the timing errors you get.
You get all the numbers up to your input from 3 or 5 till t by
from itertools import chain
t =15
threes = range(0,t+1,3) # range provides [lower..higher[ not including higher value
fives = range(0,t+1,5)
sums = sum(chain(threes,fives)) # without itertools: sums=sum(threes) + sum(fives)
print(threes)
print(fives)
print(sums)
If you want to avoid multiples, avoid chain(..), uses sets and join them together before summing them up
s = set(threes) | set(fives)
print(sum(s))
Output:
[0, 3, 6, 9, 12, 15] # threes up to 15
[0, 5, 10, 15] # fives up to 15
75 # sum of chained ones
60 # sum of set
In Python2.7 I would like to generate finite sequences for a given integer by repeating the sum of prime factors (sopfr) function over the result(s) until it reaches a prime. The following code for sopfr(n) comes from OEIS A001414.
from sympy import factorint
def sopfr(n):
return sum(p*e for p, e in factorint(n).items())
>>>sopfr(888)
46
>>>
I'd like to alter this code so that it will give this result
>>>sopfrs(888)
46 25 10 7
>>>
Where 46 = sopfr(888), 25 = sopfr(46)... and so on, until this terminates at a prime, in this case 7. I've read some on and experimented with while and for loops without luck. Experiments that, I'm sure would be good for a laugh. Forgive the novice nature of this question, any help would be appreciated.
You can use something like this example:
from sympy import factorint
def sopfr(n):
sub = []
while True:
a = sum(k*v for k,v in factorint(n).items())
if a == n:
break
else:
sub.append(a)
n = a
return sub
# Tests
a = sopfr(888)
print(a)
a = sopfr(65)
print(a)
Output:
[46, 25, 10, 7]
[18, 8, 6, 5]
It is known that there exists C(100,10) possibilities of selecting 10 different numbers from 1 to 100. each possibility is a combination like [1, 2,..., 10] or [2,3,...,11], or [11, 22, 33, .., 99, 100], as long as the 10 numbers are different.
How to list all the combinations by programming??
I don't want to write 10 Loops, python or c preferred
Using python and itertools.combinations.
Warning - Printing will take a long time
for i in itertools.combinations(xrange(1,101),10):
print i
My friend, Yujie Liu came up this idea. Using pure iteration.
# select n diff numbers from min - max
def traceArr(arr, min, max, n):
if n > 0:
for i in range(min, max-n+2):
arr1 = arr[:]
arr1.append(i)
traceArr(arr1, i+1, max, n-1)
elif n == 0:
print arr
# Demo, select 5 diff numbers from 1-10
traceArr([], 1, 10, 5);