[Tutor] Sets Python
niyanaxx95 at gmail.com
niyanaxx95 at gmail.com
Mon Dec 1 02:01:32 CET 2014
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?
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:
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")
print("Invalid command entered, the command entered was: ", command)
numInvalid = numInvalid + 1
if not command in invalidCommands :
numUnique = numUnique + 1
for line in inputFile :
theWords = line.split()
for word in theWords :
cleaned = clean(word)
if 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
# Start the program.
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.
Enter filename (default: Commands.txt): Commands.txt
Please enter a command for Arnold: dawn
The total amount of commands: 1
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...
More information about the Tutor