Python Forum
webbrowser and menu not working. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: webbrowser and menu not working. (/thread-22125.html)



webbrowser and menu not working. - sik - Oct-30-2019

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)



RE: webbrowser and menu not working. - newbieAuggie2019 - Oct-31-2019

(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,