PYTHON Your assignment is to write your first standalone python script. Here are
ID: 3807932 • Letter: P
Question
PYTHON Your assignment is to write your first standalone python script. Here are the requirements: a) Script File is: first.py b) The script will NOT important any modules c) Your script will utilize the following string definition. testString = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG" d) Your script will loop through each character of the string and print out the character and it's associated ASCII Value in Hex 3) Any variables that you create will use camel case 4) The script will be commented throughout 5) The script will contain a top comment that will provide an overview of the script: a) Name b) Version c) Functionality d) Author 6) You will submit a single file named first.py e) Extra Credit: Your script will provide a count of the number of occurrences of each character in the string A-Z and print out the result like this: A, 1 B, 1 .. Z, 1 Thanks.
Explanation / Answer
Python 3 Code:
#programs contains two functions printAsciiHex and printCharacterCount
#printAsciiHex accepts a string as argument and
#iterate through each character of string and prints it hex value
#printCharacterCount accepts a string as argument and iterate through string and
#for each character it again iterate to find the count and prints it
def printAsciiHex(testString):
print("ASCII VALUE (HEX) FOR CHARACTERS:")
for c in testString: #for each character in string
if(c!=" "): #space is excluded
print(c+","+format(ord(c), "x"),end=" ")#prints the character and its hex value
def printCharacterCount(testString):
print(" CHARACATER COUNT:")
for c in testString:#for each character in string
if(c!=" "): #if c is space excluded
charCount=0;#set count to zero
for i in testString:#iterate again through string
if(i==c):#if i=c then increment count
charCount+=1
print(c+","+str(charCount),end=" ")#print the count for c
testString="THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG" #initialize string
#call functions
printAsciiHex(testString)
printCharacterCount(testString)
Note: make sure you are using python 3 or above
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.