Ask Your Question
1

how to write error

asked 2023-02-15 05:08:46 +0100

username0808 gravatar image

updated 2023-07-07 08:51:09 +0100

FrédéricC gravatar image

Could somebody please point out where my ValueError syntax is wrong? Thank you!

def GeneratingList(n,k):
    try:
        l = [] 
        while (n <=k):
            for i in range(n):
                v.append(QQ(randint(1,k)))
            if (len(set(l)) == len(l)):
                return l
            else:
                l = []
        assert false

    except ValueError:
        print("value error, enter valid input n >= k")
edit retag flag offensive close merge delete

Comments

In the line v.append(...), I think you should replace v by l.

John Palmieri gravatar imageJohn Palmieri ( 2023-02-22 18:03:52 +0100 )edit

You don't need try: ... except: ... in this code. Simply replace assert false with your print(...) statement.

Max Alekseyev gravatar imageMax Alekseyev ( 2023-02-22 20:06:43 +0100 )edit
1

In general never use l for a variable, well one letter variables are in general not really welcome, but this "ell" is even worse, it looks like a one... You may also better try something like

my_list = [QQ(randint(1, k)) for _ in range(n)]

and then arrange somehow that the list has different elements, if this is needed...

dan_fulea gravatar imagedan_fulea ( 2023-02-23 01:52:22 +0100 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2023-02-22 18:04:59 +0100

I'm not entirely sure what you're trying to accomplish, but perhaps changing ValueError to AssertionError will fix the problem.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2023-02-15 05:08:46 +0100

Seen: 110 times

Last updated: Feb 22 '23