【问题标题】:How to repeat openeing tabs?如何重复打开标签?
【发布时间】:2021-12-04 00:40:00
【问题描述】:

这是我的代码:

times_to_repeat = 10
while times_to_repeat > 0:
    import string
    import random
    S = 6
    ran = ''.join(random.choices(string.ascii_lowercase + string.digits, k=S))
    x = ran
    print("The randomly generated string is : " + str(ran))
    import webbrowser
    webbrowser.open("prnt.sc/" + x)
    times_to_repeat -= 1

我正在尝试 prnt.sc 挑战,我希望能够打开 10 个随机字符串选项卡,以加快进程。我的问题是当我运行它时它只打开一个选项卡,但它会打印我的随机字符串十次。两者都在循环中,所以这两件事不应该重复吗?我得到“你的随机字符串是(无论字符串是什么)10 次,但我的浏览器只打开 1 个选项卡。我对 python 很陌生,所以我对循环不太了解。

【问题讨论】:

  • 我尝试了您的代码,并为我打开了 10 个浏览器(每个浏览器都有一个标签)。我了解您的情况也只是打开浏览器一次?

标签: python loops while-loop


【解决方案1】:

目前,您的代码似乎正在打开 10 种不同的浏览器。

我不太确定如何,但似乎 webbrowser.open("prnt.sc/" + x) 的内部调用使操作系统无法使用当前活动的浏览器,因为prnt.sc/ 不是网络 URL,因为@ 987654323@ 部分丢失。

只需添加协议即可:

while times_to_repeat > 0:
    import string
    import random
    S = 6
    ran = ''.join(random.choices(string.ascii_lowercase + string.digits, k=S))
    x = ran
    print("The randomly generated string is : " + str(ran))
    import webbrowser
    webbrowser.open("https://prnt.sc/" + x)
    times_to_repeat -= 1

不过,您的代码循环有点奇怪,而且有点难以阅读。此外,在 while 循环中导入库并不是一个很好的做法。

import string
import random
import webbrowser
S = 6

times_to_repeat = 4
for x in range(0, times_to_repeat):
    ran = ''.join(random.sample(string.ascii_lowercase, S))
    print("The randomly generated string is : " + str(ran))
    webbrowser.open("https://prnt.sc/" + ran)

【讨论】:

    猜你喜欢
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-13
    • 2022-11-18
    • 1970-01-01
    • 2019-08-11
    相关资源
    最近更新 更多