0

I wrote this from pseudo-code provided during class, and I've got most of it figured out. Only issue I'm running into is it's returning duplicate numbers for the first 5 'balls' and I can't at all figure out why. One of the lines in the pseudo-code I wasn't sure about was: "if that number is not in the main number sequence". I coded it like this:

if number != mainNumbers:

which could be the issue, but I'm not sure how else to code that.

from random import *

def drawing():
    balls=0
    mainNumbers=[]
    
    while balls < 5:
        number=randint(1,69)
        if number != mainNumbers:
            mainNumbers.append(number)
            balls = balls + 1

    mainNumbers.sort()
    pBall=randint(1,26)
    return mainNumbers, pBall

def main():
    print("This program simulates a user defined number of Powerball drawings\n")
    runs = int(input("What's the total number of drawings? "))
    print()
    count = 1
    while count <= runs:
        balls, pBall = drawing()
        print("Drawing: {0} - The numbers are: {1} and the Powerball is: {2}".format(count, balls, pBall))
        count = count + 1
main()
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
JTiemo
  • 1
  • 1
  • `if number not in mainNumbers:` does this solve the issue? a pretty straight forward conversion from pseudo-code I would say. so basically `if number != mainNumbers:` checks if number does not equal mainNumbers which is not the same as whether it is in them. – Matiiss Apr 13 '21 at 19:36
  • 1
    You are making this much more complicated than it needs to be. The functionality you are looking for is built in. Please try reading the [documentation(https://docs.python.org/3/library/random.html) for the `random` module. You might even try specifically looking for the word `unique`. – Karl Knechtel Apr 13 '21 at 19:37
  • `number` is an int and `mainnumbers` is a list. They will ***always*** be different. You want to check if `number` is ***not in*** `mainnumbers` which quite easily translates to `if number not in mainnumbers`... Another side not, you don't really need the variable `balls`, just use `len(mainNumbers)`... – Tomerikoo Apr 13 '21 at 19:40
  • 1
    `random.sample(range(1,26), 5)` would pretty much do what you want with the `import random` module – Gerhard Apr 13 '21 at 19:41
  • Does this answer your question? [Fastest way to check if a value exists in a list](https://stackoverflow.com/questions/7571635/fastest-way-to-check-if-a-value-exists-in-a-list) – Tomerikoo Apr 13 '21 at 19:42
  • 1
    Or even better, [Generate 'n' unique random numbers within a range](https://stackoverflow.com/questions/22842289/generate-n-unique-random-numbers-within-a-range) – Tomerikoo Apr 13 '21 at 19:46
  • @Tomerikoo Could you post your first comment as an answer? It's what ultimately helped me, and helped me to understand the issue. I'd like to mark it as such. – JTiemo Apr 13 '21 at 23:10
  • Well I'm really really happy it helped you solve the problem and better understand. But as opposed to the title, your question is basically "how to check if a number is in a list of numbers" which is answered in the link above so you can mark the question as a duplicate – Tomerikoo Apr 14 '21 at 07:04
  • Also, please don't edit solutions into the question. You are free to answer your question if you wish – Tomerikoo Apr 14 '21 at 07:06

0 Answers0