lundi 4 juillet 2022

how do i get 5 random rows from a list

I'm pretty new to python, and my problem is, I'm trying to get 5 random rows from a list with a user input value condition, i get all the values from a chosen category and supposed to get only 5 rows from that randomly.

I've tried various solutions but it doesn't help me anyway sry for my english

here's the code:

import csv import random

with open("pontos.csv","r")as f:
    r=csv.reader(f,delimiter=",")
    l=list(r)
    
    
    def ex1():
    #ex1
        for x in l:
            val=x[1],x[5]
            print(':'" ".join(val),"\n")
    

    def ex2():
        #ex2
        # escolhe cultura
            val = input("Cat:\n").split(",")

            for row in l:
                if all([x in row for x in val]):
                    print(','.join(row),"\n")
    
    def ex3():
        n=4
    # print("categorias:\n Desporto,cultura,História,Paisagem,Praia,Gastronomia,Natureza,Natureza")
        # escolhe cultura
        val = input("Cat:\n").split(",")
        for row in l:
                if all(x in row for x in val):
                    print(row)
                    
def main():
    while True:
        escolha=int(input("Menu:\n 1-mostrar todos os locais \n 2-Mostrar todos os locais de uma categoria \n 3- mostrar locais de uma categoria ao acaso \n 4-Sair \n Escolha: "))
        if escolha==1:
            ex1()
        if escolha ==2:
            ex2()
        if escolha==3:
            ex3()
            if escolha== 4:
                break
        else:
            print("escolha invalida")

main()

note: its on ex3




Aucun commentaire:

Enregistrer un commentaire