2

I'm trying to open random links using

webbrowser.open(random.choices("link1","link2","link3")

but it's showing error Check it out and help me resolve this issue.

My code:

webbrowser.open(random.choices("https://www.youtube.com/watch?v=YGf8JJZM_Yg", "https://www.youtube.com/watch?v=upsF9NULamA"))

Error I am facing:

    total = cum_weights[-1] + 0.0   # convert to float
TypeError: can only concatenate str (not "float") to str
2
  • Please show the full trace back Commented Aug 24, 2021 at 5:00
  • ` Traceback (most recent call last): File "c:\PYTHON PROGRAMMES\DUSTIN- THE VOICE ASSISTANT\dustin.py", line 233, in <module> webbrowser.open(random.choices("youtube.com/watch?v=YGf8JJZM_Yg", "youtube.com/watch?v=upsF9NULamA")) File "C:\Users\socia\AppData\Local\Programs\Python\Python39\lib\random.py", line 500, in choices total = cum_weights[-1] + 0.0 # convert to float TypeError: can only concatenate str (not "float") to str ` Commented Aug 24, 2021 at 5:09

2 Answers 2

2

Instead of:

total = cum_weights[-1] + 0.0

Try:

total = float(cum_weights[-1])

Edit:

For your other error with random.choices, try this instead:

webbrowser.open(random.choice(["https://www.youtube.com/watch?v=YGf8JJZM_Yg", "https://www.youtube.com/watch?v=upsF9NULamA"]))

random.choices take in a single argument with a parameter of a list, it isn't a *args expression in the implementation.

Sign up to request clarification or add additional context in comments.

3 Comments

This line where the exception is raised is in random.py. It is not editable. The exception is happening because arguments to random.choices() are passed incorrectly.
ERROR ` Traceback (most recent call last): webbrowser.open(random.choices(["youtube.com/watch?v=YGf8JJZM_Yg", "youtube.com/watch?v=upsF9NULamA"])) File "C:\Users\socia\AppData\Local\Programs\Python\Python39\lib\webbrowser.py", line 86, in open if browser.open(url, new, autoraise): File "C:\Users\socia\AppData\Local\Programs\Python\Python39\lib\webbrowser.py", line 603, in open os.startfile(url) TypeError: startfile: filepath should be string, bytes or os.PathLike, not list `
@AshutoshSingh Edited my answer, should work now, it was as typo
1

The arguments to random.choices() must be an iterable like list, tuple, etc like this:

random.choices(
("https://www.youtube.com/watch?v=YGf8JJZM_Yg", "https://www.youtube.com/watch?v=upsF9NULamA")
)

Notice the extra pair of parentheses. You can pass as many arguments you want but they should be given like this:

random.choices((arg1, arg2, arg3, ...))

instead of:

random.choices(arg1, arg2, arg3, ...)

Both of the below options will work:

Use random.choice() over random.choices() to select only 1 option out of multiple options.

1.

webbrowser.open(random.choice(("https://www.youtube.com/watch?v=YGf8JJZM_Yg", "https://www.youtube.com/watch?v=upsF9NULamA")))
webbrowser.open(random.choice(["https://www.youtube.com/watch?v=YGf8JJZM_Yg", "https://www.youtube.com/watch?v=upsF9NULamA"]))

3 Comments

@AshutoshSingh edited answer to add code samples, check if it helps you
no. it's not working :( ` Traceback (most recent call last) webbrowser.open(random.choices(["youtube.com/watch?v=YGf8JJZM_Yg", "youtube.com/watch?v=upsF9NULamA"])) File "C:\Users\socia\AppData\Local\Programs\Python\Python39\lib\webbrowser.py", line 86, in open if browser.open(url, new, autoraise): File "C:\Users\socia\AppData\Local\Programs\Python\Python39\lib\webbrowser.py", line 603, in open os.startfile(url) TypeError: startfile: filepath should be string, bytes or os.PathLike, not list `
@AshutoshSingh can you try using random.choice() function instead of random.choices()? It is more suitable when you need only 1 option out of a list of options. Updating my answer accordingly,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.