如何解决输入输入后,是否可以使用Python打开特定网页?
我正试图找到一种方法来编写一个脚本,该脚本接受来自用户的输入,然后,它将打开网页。到目前为止,代码如下:
jurisdiction = input("Enter jurisdiction:")
if jurisdiction = 'UK':
import webbrowser
webbrowser.open('https://www.legislation.gov.uk/new')
webbrowser.open('https://eur-lex.europa.eu/oj/direct-access.html')
elif jurisdiction = Australia:
import webbrowswer
webbrowser.open('https://www.legislation.gov.au/WhatsNew')
else:
print("Re-enter jurisdiction")
这会导致第3行出现语法错误
File "UK.py",line 3
if jurisdiction = UK
^
SyntaxError: invalid syntax**
我想知道代码中是否有任何我想念的东西?另外,还有其他方法可以做我在这里想要实现的目标吗?
解决方法
我建议阅读Python字符串比较。易于修复,但是您将对字符串比较如何在Python中起作用和不起作用具有基本了解。
英国和澳大利亚也都必须是Strings ...
并且不要在代码主体中导入webbroswer软件包。您只需要执行一次。
import webbrowser
jurisdiction = input("Enter jurisdiction:")
if jurisdiction == 'UK':
webbrowser.open('https://www.legislation.gov.uk/new')
webbrowser.open('https://eur-lex.europa.eu/oj/direct-access.html')
elif jurisdiction == 'Australia':
webbrowser.open('https://www.legislation.gov.au/WhatsNew')
else:
print("Re-enter jurisdiction")
,
更清洁的方法
import webbrowser
mapping = {'UK': ['https://www.legislation.gov.uk/new','https://eur-lex.europa.eu/oj/direct-access.html'],'Australia': ['https://www.legislation.gov.au/WhatsNew']}
jurisdiction = input("Enter jurisdiction:")
urls = mapping.get(jurisdiction)
if urls is not None:
for url in urls:
webbrowser.open(url)
else:
print("Re-enter jurisdiction")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。