Python, TypeError: 'NoneType' object is not subscriptable
Function code:
def randomize_poll_winner(connection):
poll_id = int(input("Enter poll you'd like to pick a winner for: "))
poll_options = database.get_poll_details(connection, poll_id)
_print_poll_options(poll_options)
option_id = int(input("Enter which is the winning option, we'll pick a random winner from
voters:"))
winner = database.get_random_poll_vote(connection, option_id)
print(f"The randomly selected winner is {winner[0]}.")
Second important function:
def get_random_poll_vote(connection, option_id):
with connection:
with connection.cursor() as cursor:
cursor.execute(SELECT_RANDOM_VOTE, (option_id,))
cursor.fetchone()
SQL code:
SELECT_RANDOM_VOTE = "SELECT * FROM votes WHERE option_id = %s ORDER BY RANDOM() LIMIT 1"
Logs:
Traceback (most recent call last):
File "D:\Programming\Python\Python+PostgreSQL_2.0\poll_app\app.py", line 108, in <module>
menu()
File "D:\Programming\Python\Python+PostgreSQL_2.0\poll_app\app.py", line 103, in menu
MENU_OPTIONS[selection](connection)
File "D:\Programming\Python\Python+PostgreSQL_2.0\poll_app\app.py", line 76, in randomize_poll_winner
print(f"The randomly selected winner is {winner[0]}.")
TypeError: 'NoneType' object is not subscriptable