I am trying to generate random "credit card" form. But when I do for loop, then it prints the same random generated "credit card number" everytime. Example:
Output looks like this:
2072 3600 8744 7144
2072 3600 8744 7144
2072 3600 8744 7144
2072 3600 8744 7144
2072 3600 8744 7144
And I want every "credit card num" with different numbers in it.
Code:
from random import choices
from string import digits
cc_digits = choices(digits, k=16)
cc_number = "".join(cc_digits)
space = " ".join(cc_number[i:i 4] for i in range(0, len(cc_number), 4))
for x in range(5):
print(space)
CodePudding user response:
You generate the CC number once and display it multiple times. Instead, put the CC generator in the loop.
from random import choices
from string import digits
for _ in range(5):
cc_digits = choices(digits, k=16)
cc_number = "".join(cc_digits)
space = " ".join(cc_number[i:i 4] for i in range(0, len(cc_number), 4))
print(space)
CodePudding user response:
You have put the definition of what you want to print before loop,that means it is static when loop run, so what you need to do is put them inside loop like this.
from random import choices
from string import digits
for x in range(5):
cc_digits = choices(digits, k=16)
cc_number = "".join(cc_digits)
space = " ".join(cc_number[i:i 4] for i in range(0, len(cc_number), 4))
print(space)