Python Forum
webbrowser and menu not working.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
webbrowser and menu not working.
#1
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)
Reply
#2
(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,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Webbrowser.open() causes errors ChrisOfBristol 1 843 Apr-09-2023, 08:34 PM
Last Post: deanhystad
  Menu Choice Selection not Working ChLenx79 5 1,479 Nov-23-2022, 05:56 AM
Last Post: deanhystad
Question How to get html information from a tab of my default browser opened with webbrowser? noahverner1995 2 4,336 Jan-14-2022, 10:02 AM
Last Post: noahverner1995
  How to get a URL from python 'webbrowser'? dheeraj 0 1,816 Apr-05-2021, 03:55 PM
Last Post: dheeraj
  Webbrowser jbrick97 3 51,545 Sep-27-2020, 03:58 AM
Last Post: ndc85430
  Making WebBrowser In PySide2 Harshil 0 1,876 Sep-16-2020, 05:03 PM
Last Post: Harshil
  Web scraping: webbrowser issue Truman 10 6,962 Jul-11-2018, 11:57 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020