Python Forum
If statement with arrays without activating while statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If statement with arrays without activating while statement
#1
import time #importing time fuction

Answer1 = ["Push Walls", "PUSH WALLS", "push walls", "Push walls", "push Walls"] #Array of all possible answers.
Answer2 = ["Red Green Yellow", "Red, Green, Yellow", "red green yellow", "Red green yellow", "red Green yellow", "red green Yellow", "red, green, yellow",
           "Red, green, yellow", "red, Green, yellow", "red, green, Yellow", "RED GREEN YELLOW", "RED, GREEN, YELLOW"] #Array of all possible answers.
Answer3 = ["Look Up", "look Up", "LOOK UP", "look up"] #Array of all possible answers.
lives = 3
correct = False

#introduction
def intro():
    #envelope art
    env = """
         __________________
        |\                /|
        | \              / | 
        | /\____________/\ | 
        |/                \| 
        |__________________| 
        """
    print(env)
    time.sleep(0.5)
    print("""You have been invited to the worlds leading puzzle room...
          ...You accept the invitation and are escorted to the the first room.""")
    time.sleep(0.5)

#main story
def main():
    intro() #introduction
    ######
    room1() #having separate defs for rooms makes adding / removing rooms easier.
  
    







def room1():
    global lives #making lives global so it can be accessed from outoflives()
    time.sleep(0.5)
    print("""
          On the wall in the first room, there is an alpahbet on the wall...
          ...It must be a Caesar cipher of +3. The encrypted word is: SXVK ZDOOV """)
    global correct
    correct1 = input("Decrypted Cipher:")
    
    for x in range(0,5):
        if (correct1 == Answer1[x]):
            correct = True
            print("lol")
        else:
            outoflives()
            correct = False

#called if you lose
def lose():
    print("")
    time.sleep(2)
    main()

#called if you win
def win():
    print("")
    time.sleep(2)
    main()

def outoflives():
    global lives
    if correct == False:
        while lives != 0:
            print("The walls close in...")
            break
    while lives == 0:
        print("You died!")
        lose()
        break
main()
---This is a project for school---

The arrays are showing all possible answers. In room1(), this piece of code should compare the answer you put in to all the items in the array:
 for x in range(0,5):
        if (correct1 == Answer1[x]):
            correct = True
            print("correct")
        else:
            lives = 2
            outoflives()
            correct = False
It does. But all the answers except one, will be incorrect. This activates the outflives() multiple times making the output look like:
The walls close in...
The walls close in...
The walls close in...
correct
The walls close in...

I want it to look like:
correct
if you get the correct answer and:
The walls close in...
if you get the wrong answer.

What would be the easeist way to get this to stop?

Thanks,
Dream
Reply


Messages In This Thread
If statement with arrays without activating while statement - by DreamingInsanity - Jun-20-2018, 07:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  If statement question help Gatso100 4 742 Jan-09-2024, 11:53 AM
Last Post: Pedroski55
  Can't stop if statement from executing even though False + Messy code? monkeydesu 1 1,021 Oct-17-2022, 05:46 AM
Last Post: deanhystad
  Post IF Statement Input OrphanedSoul 2 2,350 Jun-04-2021, 05:03 PM
Last Post: deanhystad
  if statement not working g0g0g1g 2 1,658 Sep-08-2020, 05:40 PM
Last Post: nilamo
  While statement explanation alkhufu2 3 2,407 Sep-02-2020, 05:46 PM
Last Post: alkhufu2
  I need help to solve this task using while statement rico4pepe 6 9,307 Apr-02-2020, 11:34 AM
Last Post: pyzyx3qwerty
  Filtering with IF Statement Mike2607 10 5,343 Nov-29-2019, 07:18 PM
Last Post: perfringo
  Trying to get an if..elif..else statement to run. Azurato 4 2,671 Jul-29-2019, 12:17 PM
Last Post: ichabod801
  Else Statement Not Working SenkouSimmer 4 3,288 Jul-22-2019, 11:42 AM
Last Post: jefsummers
  Help for newbie with if/else statement that has or statement Shevach 2 2,133 May-08-2019, 09:00 PM
Last Post: Shevach

Forum Jump:

User Panel Messages

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