MistUnleashed
MistUnleashed

Reputation: 309

Python Basics - Comparing 2 Arrays

I've been set the task to design a small game which includes a text file which is 'encrypted' and a 'decrypted' version of the text file. The user is shown the 'encrypted' list (which has been read in and appended to an array) with some clues e.g. A = # (I'm using a dictionary to store possible values of the symbols.)

My problem is: I have created the whole game but i have added an option to my menu to allow the End-User to compare the words they have substituted in with the 'decrypted' list (also read into an array) to see if they have completed the game.

I have tried the following code and have tested the game through to me being 100% sure the words were identical and the Python Shell printed "Sorry! Try Again!"

Here's The Code:

def compareFiles():
    for eachLine in range(len(rsef)):
        if rsef[eachLine] == rssf[eachLine]:
            print("Congratulations! Puzzle Solved!")
        else:
            print("Sorry! Try Again!")
            secMenu()

For Context:

secMenu() is my menu
rsef is my 'encrypted' array
rssf is the 'decrypted' array which i want to compare to.

EDIT:

Which Option Would You Like To Choose?
Option Number: 1
--------------------------------
1. View The Encrypted Words With Some Clues.

A+/084&"
A3MANA+
8N203:
,1$&
!-MN
.A7&33&
AMA71N
&-&641'2
A))85
9&330M

This is the Sorted List:

Which Option Would You Like To Choose?
Option Number: 5

ACQUIRED
ALMANAC
INSULT
JOKE
HYMN
GAZELLE
AMAZON
EYEBROWS
AFFIX
VELLUM

Upvotes: 0

Views: 198

Answers (1)

aIKid
aIKid

Reputation: 28292

Here's to check if all items in both lists are identical:

def compareFiles():
    if rsef == rssf:
        print("Congratulations! Puzzle Solved!")
    else:
        print("Sorry! Try Again!")
        secMenu()

If you insist on looping, the one below :)

def compareFiles():
    for eachLine in range(len(rsef)):
        if rsef[eachLine] != rssf[eachLine]:
            print("Sorry! Try Again!")
            secMenu()
            return 0 #exit the function, use this if you need it.

        print("Congratulations! Puzzle Solved!")

Upvotes: 2

Related Questions