[Tutor] Sets PythonTutor Digest, Vol 130, Issue 1
Dino Bektešević
ljetibo at gmail.com
Mon Dec 1 13:15:00 CET 2014
Hey,
> invalidCommands = 0
> command = ("left", "right", "up", "down")
> numUnique = 0
> numInvalid = 0
> invalidCommands = set()
It seems you have defined invalidCommads as borh of int type and a set
type.
Also a nice trick for multiple options in the future is:
If command is in ("up","down","left","right"):
print command
Of course thos wont worj now vecause yoy have ti keep track of tge
counters, but I personaly like it and find it usefull.
Good luck,
Dino
> Message: 2
> Date: Mon, 1 Dec 2014 01:01:32 +0000
> From: <niyanaxx95 at gmail.com>
> To: "=?utf-8?Q?tutor at python.org?=" <tutor at python.org>
> Subject: [Tutor] Sets Python
> Message-ID: <547bbf1d.91268c0a.220f.3741 at mx.google.com>
> Content-Type: text/plain; charset="utf-8"
>
> I am not understanding how I am supposed to do the highlighted things on
the problem statement. If someone could please direct me on the steps to go
about doing what is highlighted. Have do I read each command, by making
the program read each line?? How do I add the invalid commands into a set?
When printing the command is invalid, do I simply do as I did with the
valid commands print?
>
>
> 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
>
>
> After you read each command, make sure it is valid (included in the set
of valid commands).
>
> 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.
>
> If the command is invalid, add it to a set of invalid commands.
>
> Keep a count of:
>
> The total number of commands read
>
>
> The total number of valid commands read
>
>
> The total number of invalid commands read
>
>
> 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
>
>
> Print the number of unique invalid commands read
>
>
> Print the set of invalid commands.
>
>
> The format of the input files is:
>
> Each line in the file contains a command, not all command are valid
>
>
>
>
>
> My Code So Far:
>
> def main() :
> # Define main variables
> leftCount = 0
> rightCount = 0
> upCount = 0
> downCount = 0
> commandCount = 0
> invalidCommands = 0
> command = ("left", "right", "up", "down")
> numUnique = 0
> numInvalid = 0
> invalidCommands = set()
>
> filename = input("Enter filename (default: Commands.txt): ")
> if len(filename) == 0 :
> filename = "Commands.txt"
> inputFile = open(filename, "r")
>
>
> command = input("Please enter a command for Arnold: ")
> if command in command :
> commandCount = commandCount + 1
> print("The total amount of commands: ", commandCount)
> 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")
> else :
> print("Invalid command entered, the command entered was: ",
command)
> numInvalid = numInvalid + 1
> if not command in invalidCommands :
> numUnique = numUnique + 1
> invalidCommands.add(command)
>
>
>
>
>
> 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.")
>
>
>
> # Cleans a string by making letters lowercase and removing characters
that are not letters
> # @param string the string to be cleaned
> # @return the cleaned string
> def clean(string) :
> result = ""
> for char in string :
> if char.isalpha() :
> result = result + char
> return result.lower()
>
>
>
>
>
> # Start the program.
> main()
>
>
>
> What Happens When Ran:
>
> 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit
(Intel)]
> Python Type "help", "copyright", "credits" or "license" for more
information.
> [evaluate CommandsDraft.py]
> 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'
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <
http://mail.python.org/pipermail/tutor/attachments/20141201/fecd2537/attachment.html
>
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> https://mail.python.org/mailman/listinfo/tutor
>
>
> ------------------------------
>
> End of Tutor Digest, Vol 130, Issue 1
> *************************************
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20141201/2e09024e/attachment-0001.html>
More information about the Tutor
mailing list