问题描述
我正试图找到一种方法来编写一个脚本,该脚本接受来自用户的输入,然后,它将打开网页。到目前为止,代码如下:
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")