jeudi 22 décembre 2022

Is there a better way to write this random list function?

Traceback (most recent call last):
  File "main.py", line 9, in <module>
    rand = random.randint(0,place)
  File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/random.py", line 248, in randint
    return self.randrange(a, b+1)
TypeError: can only concatenate str (not "int") to str

^This is my error. I'm not sure why this code isn't working as I'm new to Python. I'm trying to write a code that detects how big a list is, then selects a random item from the list.

import random

greetings = ["hello","hola","bonjour","privet","ciao","guten tag","shalom"]

place = 0
for i in greetings:
  place = i

rand = random.randint(0,place)
print(greetings[rand])

^This is what I have so far. Any help would be appreciated




Aucun commentaire:

Enregistrer un commentaire