Python random.choice() fails to work in a function
Question:
I’m having trouble getting Pythons random.choice to work
Here’s the working code:
import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
print("Password Generator")
user_amount_letters = int(input("How many letters would you like in your password?n"))
user_amount_symbols = int(input("How many symbols?n"))
user_amount_numbers = int(input("How many numbersn"))
characters = []
for i in range(user_amount_letters):
characters.append(random.choice(letters))
for i in range(user_amount_numbers):
characters.append(random.choice(numbers))
for i in range(user_amount_symbols):
characters.append(random.choice(symbols))
random.shuffle(characters)
password = "".join(characters)
print(password)
Here’s the same code within a function:
.
.
.
def randomise_user_password(letters, symbols, numbers):
user_letters = letters
user_symbols = symbols
user_numbers = numbers
characters = []
for i in range(user_letters):
characters.append(random.choice(letters))
for i in range(user_numbers):
characters.append(random.choice(numbers))
for i in range(user_symbols):
characters.append(random.choice(symbols))
random.shuffle(characters)
password = "".join(characters)
return password
print(randomise_user_password(user_amount_letters, user_amount_symbols, user_amount_numbers))
The problem seems to be with random.choice, heres the error for reference:
3.8.12/lib/python3.8/password_generator.py", line 288, in choice
i = self._randbelow(len(seq))
TypeError: object of type 'int' has no len()
I understand what the error is telling me, but have no clue how to resolve it.
Answers:
The parameters of the function letters, symbols, numbers are named like the arrays. So you are trying to make a random choice of a int. Pas by parameter the list of letters, symbols, numbers too.
import random
lettersList = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbersList = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbolsList = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
user_amount_letters = int(input("How many letters would you like in your password?n"))
user_amount_symbols = int(input("How many symbols?n"))
user_amount_numbers = int(input("How many numbersn"))
def randomise_user_password():
password = []
for i in range(0, user_amount_letters):
password.append(random.choice(lettersList))
for i in range(0, user_amount_symbols):
password.append(random.choice(symbolsList))
for i in range(0, user_amount_numbers):
password.append(random.choice(numbersList))
random.shuffle(password)
password = "".join(password)
return password
print(randomise_user_password())
Input
3 letters, 2 symbols, 3 numbers
Outputs
wAp8*53$
The arguments of your method are named the same as your lists.
Just change up the names of your lists like this:
import random
lettersList = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbersList = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbolsList = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
user_amount_letters = int(input("How many letters would you like in your password?n"))
user_amount_symbols = int(input("How many symbols?n"))
user_amount_numbers = int(input("How many numbersn"))
def randomise_user_password(letters, symbols, numbers):
user_letters = letters
user_symbols = symbols
user_numbers = numbers
characters = []
for i in range(user_letters):
characters.append(random.choice(lettersList))
for i in range(user_numbers):
characters.append(random.choice(numbersList))
for i in range(user_symbols):
characters.append(random.choice(symbolsList))
random.shuffle(characters)
password = "".join(characters)
return password
print(randomise_user_password(user_amount_letters, user_amount_symbols, user_amount_numbers))
Input: 5 5 5
Output: mHEWsvmnYxfGQcu
I’m having trouble getting Pythons random.choice to work
Here’s the working code:
import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
print("Password Generator")
user_amount_letters = int(input("How many letters would you like in your password?n"))
user_amount_symbols = int(input("How many symbols?n"))
user_amount_numbers = int(input("How many numbersn"))
characters = []
for i in range(user_amount_letters):
characters.append(random.choice(letters))
for i in range(user_amount_numbers):
characters.append(random.choice(numbers))
for i in range(user_amount_symbols):
characters.append(random.choice(symbols))
random.shuffle(characters)
password = "".join(characters)
print(password)
Here’s the same code within a function:
.
.
.
def randomise_user_password(letters, symbols, numbers):
user_letters = letters
user_symbols = symbols
user_numbers = numbers
characters = []
for i in range(user_letters):
characters.append(random.choice(letters))
for i in range(user_numbers):
characters.append(random.choice(numbers))
for i in range(user_symbols):
characters.append(random.choice(symbols))
random.shuffle(characters)
password = "".join(characters)
return password
print(randomise_user_password(user_amount_letters, user_amount_symbols, user_amount_numbers))
The problem seems to be with random.choice, heres the error for reference:
3.8.12/lib/python3.8/password_generator.py", line 288, in choice
i = self._randbelow(len(seq))
TypeError: object of type 'int' has no len()
I understand what the error is telling me, but have no clue how to resolve it.
The parameters of the function letters, symbols, numbers are named like the arrays. So you are trying to make a random choice of a int. Pas by parameter the list of letters, symbols, numbers too.
import random
lettersList = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbersList = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbolsList = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
user_amount_letters = int(input("How many letters would you like in your password?n"))
user_amount_symbols = int(input("How many symbols?n"))
user_amount_numbers = int(input("How many numbersn"))
def randomise_user_password():
password = []
for i in range(0, user_amount_letters):
password.append(random.choice(lettersList))
for i in range(0, user_amount_symbols):
password.append(random.choice(symbolsList))
for i in range(0, user_amount_numbers):
password.append(random.choice(numbersList))
random.shuffle(password)
password = "".join(password)
return password
print(randomise_user_password())
Input
3 letters, 2 symbols, 3 numbers
Outputs
wAp8*53$
The arguments of your method are named the same as your lists.
Just change up the names of your lists like this:
import random
lettersList = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbersList = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbolsList = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
user_amount_letters = int(input("How many letters would you like in your password?n"))
user_amount_symbols = int(input("How many symbols?n"))
user_amount_numbers = int(input("How many numbersn"))
def randomise_user_password(letters, symbols, numbers):
user_letters = letters
user_symbols = symbols
user_numbers = numbers
characters = []
for i in range(user_letters):
characters.append(random.choice(lettersList))
for i in range(user_numbers):
characters.append(random.choice(numbersList))
for i in range(user_symbols):
characters.append(random.choice(symbolsList))
random.shuffle(characters)
password = "".join(characters)
return password
print(randomise_user_password(user_amount_letters, user_amount_symbols, user_amount_numbers))
Input: 5 5 5
Output: mHEWsvmnYxfGQcu