1

Hi, can you help me with the following error? I know it sounds very stupid. When doing an if / else conditional, I get a syntax error. Would appreciate your help.

 def request(self, email, password):
   session = requests.session()
   login_page = session.get(login_url)
   login_soup = BeautifulSoup(login_page.text,'html5lib' )
   e = login_soup.find('input', {'name':'login_form[_token]'})
   csrftok = e['value']
   session.post(login_url,data={'login_form[name]': user,'login_form[password]': passw,'login_form[redirect_url]': '/','login_form[_token]': csrftok})
   membership = session.get(member_url).text()
   if "Free" in membership:
       output().screen(email, password, case = "Free")
       else:
           output().screen(email, password, case = "Premium")
   else:
       output().screen(email, password, case = "Not Working")
3
  • 4
    You have else without if. Commented Nov 12, 2019 at 13:51
  • 1
    One if, two else? Commented Nov 12, 2019 at 13:52
  • It looks like you'll need to learn about elif to achieve the behaviour you're interested in here Commented Nov 12, 2019 at 13:52

4 Answers 4

2

The third line here...

if "Free" in membership:
    output().screen(email, password, case = "Free")
    else:  # <----- Problematic line
        output().screen(email, password, case = "Premium")
else:
    output().screen(email, password, case = "Not Working")

has no corresponding if statement. You need another if statement, otherwise remove that line.

Sign up to request clarification or add additional context in comments.

1 Comment

I guess what the OP is trying to do is an "else if", you may include an example to show how it should be done.
1

I guess you want to do if elif else

if "Free" in membership:
   output().screen(email, password, case="Free")
elif "Premium" in membership:
   output().screen(email, password, case="Premium")
else:
   output().screen(email, password, case="Not Working")

Comments

0
if "Free" in membership:
   output().screen(email, password, case = "Free")
>>>else:
       output().screen(email, password, case = "Premium")
else:
       output().screen(email, password, case = "Not Working")
  1. Indentation
  2. There is a stray 'else'

Comments

0

In python it is really important to respect indentation and to have a well organized code. As you have an else statement without an if And for {{else if}} you can use elif So you have to follow the following pattern:

if c:
   code
elif c1:
   code1
else:
   code2

so your code has to be like this:

 def request(self, email, password):
   session = requests.session()
   login_page = session.get(login_url)
   login_soup = BeautifulSoup(login_page.text,'html5lib' )
   e = login_soup.find('input', {'name':'login_form[_token]'})
   csrftok = e['value']
   session.post(login_url,data={'login_form[name]': user,'login_form[password]': passw,'login_form[redirect_url]': '/','login_form[_token]': csrftok})
   membership = session.get(member_url).text()
   if "Free" in membership:
       if <<condition here>>:
           output().screen(email, password, case = "Free")
       else:
           output().screen(email, password, case = "Premium")
   else:
       output().screen(email, password, case = "Not Working")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.