Start New Discussion within our Software Development Community
#from sys import exit
#
#medical_kit = (0)
#
#def room_4b():
#    global medical_kit
#    if medical_kit >= 1:
#        print "You have a medical kit."
#    else medical_kit <= 0:
#        print "If only you could revive him somehow."
#
####Here is the error I get when script is ran in PowerShell####
#PS C:\test> python gameerror.py
#  File "gameerror.py", line 9
#    else medical_kit <= 0:
#                   ^
#SyntaxError: invalid syntax

I am trying to get the function room_4b() to check the medical kit variable's value. The line "if medical_kit >= 1:" seems to be working fine for me, however, the line "else medical_kit <= 0:" doesn't want to work for me. I know I'm missing something here and I've scrutinized the syntax to make sure there was no mistake. I thank you in advance for any help here.

Almost always nothing after else: .
You have some cases like a ternary operator,list comprehension where else is not bare.
You can you use if or elif in this case.

It is almost always best to try avoiding global statement.
Pass variable in as an argument and in most cases return result out(insted of print in function)
So these 3 function do the same.

def room_4b(medical_kit):
    if medical_kit >= 1:
        print "You have a medical kit."
    elif medical_kit <= 0:
        print "If only you could revive him somehow."
    else:
        print 'Someting else'

medical_kit = 0
room_4b(medical_kit)
def room_4b(medical_kit):
    if medical_kit >= 1:
        print "You have a medical kit."
    else:
        print "If only you could revive him somehow."

medical_kit = 0
room_4b(medical_kit)
def room_4b(medical_kit):
    if medical_kit >= 1:
        return "You have a medical kit."
    return  "If only you could revive him somehow."

medical_kit = 0
print room_4b(medical_kit)
This article has been dead for over six months. Start a new discussion instead.