Ask Your Question
1

how to write error

asked 2 years ago

username0808 gravatar image

updated 1 year ago

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")
Preview: (hide)

Comments

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

John Palmieri gravatar imageJohn Palmieri ( 2 years ago )

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

Max Alekseyev gravatar imageMax Alekseyev ( 2 years ago )
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 ( 2 years ago )

1 Answer

Sort by » oldest newest most voted
0

answered 2 years ago

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

Preview: (hide)
link

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: 2 years ago

Seen: 122 times

Last updated: Feb 22 '23