Can someone please explain to me why this doesn't work?
def webpage():
import webbrowser
web1="http://www.bbc.co.uk/"
web2="http://en.wikipedia.org/wiki/Main_Page"
web3="http://www.nasa.gov/"
answer=(input("""
1 To open BBC
2 To open Wikipedia
3 To open NASA
""")
if answer==1:
webbrowser.open(web1)
elif answer==2:
webbrowser.open(web2)
elif answer==3:
webbrowser.open(web3)
(Oct-30-2019, 11:03 PM)sik Wrote: [ -> ]Can someone please explain to me why this doesn't work?
answer=(input("""
1 To open BBC
2 To open Wikipedia
3 To open NASA
""")
if answer==1:
webbrowser.open(web1)
elif answer==2:
webbrowser.open(web2)
elif answer==3:
webbrowser.open(web3)
Hi!
Input() gets what you type as strings, so I would then transform the values into strings ('1', '2', '3', instead of 1, 2, 3):
if answer=='1':
webbrowser.open(web1)
elif answer=='2':
webbrowser.open(web2)
elif answer=='3':
webbrowser.open(web3)
Also, you have missed the last parenthesis in answer (with the multiline input() function), and finally, you have to call the function that you have created on the last line (webpage()):
def webpage():
import webbrowser
web1="http://www.bbc.co.uk/"
web2="http://en.wikipedia.org/wiki/Main_Page"
web3="http://www.nasa.gov/"
answer=(input("""
1 To open BBC
2 To open Wikipedia
3 To open NASA
"""))
if answer == '1':
webbrowser.open(web1)
elif answer == '2':
webbrowser.open(web2)
elif answer == '3':
webbrowser.open(web3)
webpage()
I checked and now it works.
All the best,