Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 If statement with arrays without activating while statement DreamingInsanity Silly Frenchman Posts: 41 Threads: 12 Joined: Jun 2018 Reputation: 1 Likes received: 0 #1 Jun-20-2018, 07:14 PM ```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 ichabod801 Bunny Rabbit Posts: 3,805 Threads: 82 Joined: Sep 2016 Reputation: 229 Likes received: 1155 #2 Jun-20-2018, 08:39 PM (This post was last modified: Jun-20-2018, 08:40 PM by ichabod801. Edited 1 time in total. Edit Reason: should on myself ) Well, you could use the in operator, which sees if any one of the items in a container equals a specific value: ```if correct1 in Answer1: ```Then you could get rid of the for loop and just have the if/else clause. However, it appears you are only using Answer1 to handle different ways to capitalize 'push walls'. This is generally done with the lower method of the string, which converts it to lower case: ```if correct1.lower() == 'push walls': ```Your use of global is problematic. You'll run into problems with multiple different variables using slightly different names. You should pass values to the functions as parameters, and get results out of them with return statements. This way you can pass values between functions without clogging the global namespace. If you want to expand this you are going to have multiple functions doing similar things, and the code is going to be hard to maintain. If you find a problem in one function, you'll have to correct it in all of the similar functions. Much better is to keep the rooms and the information about the rooms in some sort of data structure, and to have functions that can operate on the information for any room, and keep track of where you are in the rooms. See the text adventures tutorial link in my signature for an example of how to do this. Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures DreamingInsanity Silly Frenchman Posts: 41 Threads: 12 Joined: Jun 2018 Reputation: 1 Likes received: 0 #3 Jun-21-2018, 05:21 PM (This post was last modified: Jun-21-2018, 05:35 PM by DreamingInsanity.) Thanks for that, I will try this out soon! Dream It works, thanks! « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Trying to get an if..elif..else statement to run. Azurato 4 239 Jul-29-2019, 12:17 PM Last Post: ichabod801 Else Statement Not Working SenkouSimmer 4 300 Jul-22-2019, 11:42 AM Last Post: jefsummers Help for newbie with if/else statement that has or statement Shevach 2 249 May-08-2019, 09:00 PM Last Post: Shevach syntax error in an if statement at the else ? Just_started 2 268 Mar-07-2019, 08:35 PM Last Post: Just_started Decision statement problems erfanakbari1 2 315 Mar-06-2019, 12:27 AM Last Post: woooee How to print a statement if a user's calculated number is between two floats Bruizeh 2 316 Feb-10-2019, 12:21 PM Last Post: DeaD_EyE if/else statement only outputs else statement regardless of input KameronG 2 316 Feb-08-2019, 08:04 AM Last Post: KameronG While loop and If Statement farzankh 3 435 Jan-27-2019, 10:13 PM Last Post: stullis A three if statement problem I am having WarblingPasta 1 428 Nov-01-2018, 03:04 PM Last Post: j.crater How to force print statement to print on one line wlsa 4 557 Oct-28-2018, 09:39 PM Last Post: wavic

Forum Jump:

Users browsing this thread: 1 Guest(s)