# [Tutor] Sets Python

Alan Gauld alan.gauld at btinternet.com
Mon Dec 1 12:07:48 CET 2014

```On 01/12/14 01:01, niyanaxx95 at gmail.com wrote:
>  Have do I read each command, by making the program read each line??

Yes, each line of the file contains one command.
read the file line by line.
Do you know how to do that?

> How do I add the invalid commands into a set?

Do you know how to create and use sets in Python?
This exercise seems to be based on using sets so I would expect your
teacher/tutorial to have explained sets before setting the problem.

>  When printing the command is invalid, do I simply do as I
>  did with the valid commands print?

You just use the print function as usual.

> Problem Statement:
>
> We are going to write a program that reads an input file of commands
> (Commands.txt) to Arnold the Robot.
>
> The four valid command are:
>     Up
>     Down
>     Left
>     Right

You need to store these four strings in a set.

> After you read each command, make sure it is valid (included in the
> */set/*of valid commands).

Then check the input from the file against the contents of your set.

> If the command is valid, print “Valid command entered, the command was:”
> and the command.
> If the command is invalid, print “Invalid command entered, the command
> was:” and the command.

Use an if/else statement to print the valid or invalid result.

> If the command is invalid, add it to a */set/*of invalid commands.

And if its invalid add the new command to a new set of invalid commands,
which starts out empty.

> Keep a count of:
>     The total number of commands read
>     The total number of valid commands read
>     The total number of invalid commands read

You will need counters for these and increment them after each command.

> After all of the commands have been read:
>     Print the total number of commands read
>     Print the total number of valid commands read
>     Print the total number of invalid commands read

These are just the counters above

>     Print the number of unique invalid commands read
>     Print the set of invalid commands.

This is just the size of the invalid set and the members of that set.

> def main() :
>     # Define main variables
>         leftCount = 0
>         rightCount = 0
>         upCount = 0
>         downCount = 0

You aren't asked to count each individual command just the total of
valid commands so you could simplify this by having one variable instead
of 4.

>         commandCount = 0
>         invalidCommands = 0
>         command = ("left", "right", "up", "down")

This is not a set. You were asked to use a set.

>         numUnique = 0
>         numInvalid = 0

You already have invalidCommands what is the purpose
of this new variable?

>         invalidCommands = set()

Is it because this wipes out the original variable?

>         filename = input("Enter filename (default: Commands.txt): ")
>         if len(filename) == 0 :
>                filename = "Commands.txt"
>         inputFile = open(filename, "r")

So far so good.

>         command = input("Please enter a command for Arnold: ")

But this is wrong. The commands are in the file so you need to loop over
the file reading and processing each line.

>         if command in command :

Here you are trying to use two variables with the same name - that won;t
work.
You need to rename your set of valid commands - to validCommands maybe?

>                       commandCount = commandCount + 1
>                       print("The total amount of commands: ", commandCount)

You only need to print the totals at the end.
But you are supposed to print a message:

> If the command is valid,
> print “Valid command entered, the command was:”
> and the command.

In programming it is important to stick to the specification you have
been given and not try to interpret the problem in your own way.

>         if command == "up" :
>                       upCount = upCount + 1
>                       print("Valid command entered, the command was: up")
>         elif command == "down" :
>                       downCount = downCount + 1
>                       print("Valid command entered, the command was: down")
>         elif command == "left" :
>                leftCount = leftCount + 1
>                print("Valid command entered, the command was: left")
>         elif command == "right" :
>                rightCount = rightCount + 1
>                print("Valid command entered, the command was: right")

This is OK but its more complicated than it needs to be to meet the
specification.

>         else :
>                print("Invalid command entered, the command entered was:
> ", command)
>                numInvalid = numInvalid + 1
>                if not command in invalidCommands :
>                       numUnique = numUnique + 1

Again this looks ok.

>         for line in inputFile :
>                theWords = line.split()
>                for word in theWords :
>                       cleaned = clean(word)
>                if cleaned != "" :
>                       numUnique.append(cleaned)
>         print("The file contains", len(numUnique), "unique words.")

This is all a bit odd. Its not clear what purpose it brings.
It is almost what you should be doing at the top of your
function to read the commands from the file.

But it all looks a bit like overkill for this task.
I'd just use something like:

for line in inputFile:
command = line.strip().lowercase()

All the cleaning stuff isn't really relevant since you should be testing
for valid input in your if/else code above.

> Enter filename (default: Commands.txt): Commands.txt
> Please enter a command for Arnold: dawn
> The total amount of commands:  1
> Invalid
> Traceback (most recent call last):
>    File "C:\Program Files (x86)\Wing IDE 101
> 5.0\src\debug\tserver\_sandbox.py", line 70, in <module>
>    File "C:\Program Files (x86)\Wing IDE 101
> 5.0\src\debug\tserver\_sandbox.py", line 52, in main
> builtins.AttributeError: 'int' object has no attribute 'append'

That's because in the file reading bit, at the end, you have

numUnique.append

but numUnique is an integer. You can't append to integers.

But that whole section is too complex and in the wrong place.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

```