-2

sorry for the horrible title, I have no idea how I'd describe my problem.

I'm trying to generate a code that contains 15 characters. 7 of these are already given and will be called the "base". The other 8 have to be random from 0 to 9.

This is my code

numList = "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"

whatSerial = input("Serial to generate?").upper()

def redmi4A(numList):
    return "8655920" + numList + numList + numList + numList + numList + numList + numList + numList + " -> Redmi 4A"
if whatSerial == "4A":
    for i in numList:
        print(redmi4A(i))

however the output looks like this:

865592011111111 -> Redmi 4A
865592022222222 -> Redmi 4A
865592033333333 -> Redmi 4A
865592044444444 -> Redmi 4A
865592055555555 -> Redmi 4A
865592066666666 -> Redmi 4A
865592077777777 -> Redmi 4A
865592088888888 -> Redmi 4A
865592099999999 -> Redmi 4A
865592000000000 -> Redmi 4A

as you can see it generates the base + 11111, 2222, 3333 etc. I want the numbers after the base to output all possible solutions, so the numbers after the base would be in a random order.

verbids
  • 55
  • 6
  • 1
    N.b. `numList` defined in the first line is a tuple, not a list, and the `numList` argument to `redmi4A` is a string, also not a list. – wjandrea Jun 23 '19 at 01:09

2 Answers2

3

TRY:-

import random

numList = "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"

whatSerial = input("Serial to generate?").upper()

def redmi4A(opt):
    return "8655920" + "".join([random.choice(numList) for x in range(8)]) + " -> Redmi 4A"
if whatSerial == "4A":
    for i in numList:
        print(redmi4A(i))

OUTPUT:-

865592087748606 -> Redmi 4A
865592065496599 -> Redmi 4A
865592061039159 -> Redmi 4A
865592004047509 -> Redmi 4A
865592089008612 -> Redmi 4A
865592065068787 -> Redmi 4A
865592015446593 -> Redmi 4A
865592095893322 -> Redmi 4A
865592074954808 -> Redmi 4A
865592019366958 -> Redmi 4A

There was one logical error in your code too, in function redmi4A() the argument name is the same as the name of the tuple numList which causes shadow name issues, and overrides the global tuple by the argument passed to the function(as both are under the same name). So, I changed the variable name to opt you can name it something else.

Vasu Deo.S
  • 1,820
  • 1
  • 11
  • 23
2

To generate random numbers, use the random module. Here's an example of how to generate a random number between 0 and 9:

import random

random_num = random.randint(0, 9)

So your code would need to be changed to this:

import random

whatSerial = input("Serial to generate?").upper()

def redmi4A():
    return "8655920" + ''.join([str(random.randint(0, 9)) for _ in range(8)]) + " -> Redmi 4A" # 8655920 + 8 random digits + " -> Redmi 4A"

if whatSerial == "4A":
    for i in range(10):
        print(redmi4A())

I hope this helps. If not, please try to clarify your question a bit more.