-1

I'm making a game with a secret character to defeat an enemy but the error happens during the if statement just copy and paste this code in python to access the error for the first question it doesn't matter second one has to be Agent1 then you have to go left`enter code here

mimic=1
level=1
lives=0
weapon=0
squid=0
print ("Welcome to this python game in development \n . . . \nenjoy")
pointless = input("Are you a boy or a girl\n")
Name = input("What's your charater's name? \n")
if Name=="Agent1" or "Agent2" or "Agent3" or "Agent4" or "Agent5" or "Agent6" or "Agent7" or "Agent8":
    squid=1
print("Hello",Name,"you will embark on a grand adventure full of monsters your quest is to save the princess")
def part2():
    print("To your left, you see a level one slime")
    print("To your right, there is less forest than before .")
    print("There is a wall of trees directly in front of you.")
    print("Behind you is where you were before.\n")
    response2 = input("What direction do you move?\nleft/right/forward/backward\n")
    if response2 == "left":
        print("The monster has eaten you, you are dead")
        quit()
    elif response2 == "right":
        print("You head deeper into the forest.\n")
        Choice2=Choice2+1
    elif response2 == "forward":
        print("You hugged the trees you feel happy but you have to go.")
        part2()
    elif response2 == "backward":
        part1()
    else:
        print("I didn't understand that.\n")
def part1(): 
    print("To your left, you see a little girl, you should leave them alone.")
    print("To your right, there is more forest.")
    print("There is a rock wall directly in front of you.")
    print("Behind you is the forest exit.\n")
    response = input("What direction do you move?\nleft/right/forward/backward\n")
    if response == "left"and mimic==1 and squid==1:
        print("testy test.")
        mimic=mimic-1
        part2()
    elif response == "left":
        print("The police are after you now, " + Name + ", you knew it was a mimic but they didn't believe you.")
        quit()
    elif response == "right":
        print("You head deeper into the forest.\n")
        part2()
    elif response == "forward":
        print("You cannot scale the wall.\n")
        response = ""
    elif response == "backward":
        print("You leave the forest. Goodbye, " + Name + ".")
        quit()
    else:
        print("I didn't understand that.\n")

part1()
print("To be continued ")

please tell me how to solve this Thank you

1 Answers1

0

There is a problem with your statement if Name=="Agent1" or "Agent2".... This is equivalent to if (Name=="Agent1") or ("Agent2"), so it will always be True, since the truthness of a string (here "Agent2") is True unless the string is empty ""

You need to write it like this :

if Name=="Agent1" or Name=="Agent2"

Or, more elegant :

if Name in ["Agent1", "Agent2"]
Phoenixo
  • 2,071
  • 1
  • 6
  • 13
  • I changed the code to the more elegant one and still get this error. Traceback (most recent call last): File "O:\stuff\test.py", line 56, in part1() File "O:\stuff\test.py", line 37, in part1 if response == "left"and mimic==1 and squid==1: UnboundLocalError: local variable 'mimic' referenced before assignment – Virus0123 Mar 12 '20 at 12:46
  • You need to add `global mimic` in your function definition, somewhere after `def part1():` and before `if response == "left"and mimic==1 and squid==1:` – Phoenixo Mar 12 '20 at 15:26