-1

So I'm new to coding and I need some help with this assignment, this is my code

I can't get the 'import webbrowser' to work (SyntaxError) under an if statement since it's not made for that. I was wondering if there's any way I could either modify it or change it while still being able to open a link. This assignment is due friday, so I'd really appreciate any help

  • 1
    please don't paste screenshot but post the actual code, btw any reason for using python2? switch to python3 as python2 is long dead. – Krishna Chaurasia Mar 03 '21 at 05:56
  • Also, if it is 'due' friday, you should not ask the answer for your assignments. – nizarcan Mar 03 '21 at 05:58
  • Hi sorry! I didn’t know what was the right way to do this so I chose this one. I’ll remember it next time thanks! And I’m using python2 bc of the app I use; pycharm. My computer doesn’t work w visual studio so I use that instead! I’ll see how I can switch it to python3 tho :) – Sara Barrera Mar 04 '21 at 06:27
  • Also @nizarcan, yes it’s due Friday and I’m asking for help bc my teacher didn’t even know the code to open a website and stackoverflow was the one that helped me with that.... no need to worry as my teacher knows abt this and even encouraged me to find the answers when I was in the doubt through the internet. I’m emailing him rn abt my current issue ok thanks – Sara Barrera Mar 04 '21 at 06:29

3 Answers3

2

You cannot use "is" with a literal; if x is 'yes': throws the the SyntaxError.

You have to use if x == 'yes': instead. Your code should be like

def album_link():
    print('Check them out')
    x = input('yes or no: ')
    if x == 'yes':
        import webbrowser
        webbrowser.open('http://stackovefflow.com', new=2)
    else:
        x == 'no'  # does nothing
        print('OK')

This is a Python 3 sample which hasn't raw_input anymore.

xqt
  • 280
  • 1
  • 11
  • "You cannot use "is" with a literal; if x is 'yes': throws the the SyntaxError." Not true, though your answer is most likely what op wants. – Nerveless_child Mar 03 '21 at 06:46
  • 1
    Right. Should mean: 'You cannot use "is" with a literal inside an if -statement", e.g. if 'foo' is 'bar': print('baz') will fail. – xqt Mar 03 '21 at 14:24
  • This is what I mean, https://stackoverflow.com/questions/132988/is-there-a-difference-between-and-is – Nerveless_child Mar 03 '21 at 15:12
  • Thank you for this!!! I acc realized this morning and changed it to ==. However I’m still having an issue, I changed the order of the ‘if’ and ‘else’ so the import webbrowser wouldn’t cause a problem w being under a method. It opens up the website when I say yes, however it does it too when I say no... :// – Sara Barrera Mar 04 '21 at 06:31
  • Also I forgot to mention! I got the raw_input to work by just messing around with it and changed it to input so that still works in python3 (checked it on the school computers :D ) – Sara Barrera Mar 04 '21 at 06:35
0

With a few exceptions, Python expects import statements at the toplevel of your program, not within an if statement:

import webbrowser

def album_link(self):
    # ...
Samuel Hunter
  • 527
  • 2
  • 11
  • I tried this multiple times sadly it just still wouldn’t open up the website :/ I’ll see if I could make it work that way but thank you! :) – Sara Barrera Mar 04 '21 at 06:32
0

Thank you so much to everyone who helped!! The '==' answer and adding the import webbrowser at the beginning made the code work! This is how it turned out

`

import webbrowser

class Albums:

def __init__(self, name, artist, genre, release, spotify):
    self.name = name
    self.artist = artist
    self.genre = genre
    self.release = release
    self.spotify = spotify
    

def album_desc(self):
    # other way return'The album ' + "{}".format(self.name) + ' was made by ' + "{}".format(self.artist)
    return('The album ' + self.name + ' made by ' + self.artist + ', is a(n) ' + self.genre + ' album released in ' + "{}".format(self.release) + ' with over ' + "{}".format(self.spotify) + ' spotify listeners!')


def album_link(self):
    print('You can check them out here!')

    x = str(raw_input("yes or no: "))

    if x == 'no':
        print('Ok :)! Thank you for checking it out')

    else:
        x == 'yes'
        webbrowser.open('http://themuffinmanbops.carrd.com', new=2)

`