View the step-by-step solution to:

Part A In Python, there are two main rules for naming variables.

Strings

This assignment focuses on indexing, slicing, and iterating over strings as well as Python’s standard string functions.

Part A

In Python, there are two main rules for naming variables.

  • The first character cannot be a number.
  • Only letters, numbers, and the underscore character are allowed.

Write a program that prompts users to enter a potential variable name and tells them whether or not it is valid. Your program should consist of two functions. The first should take one argument—the potential variable name, validate the name, and return Boolean Trueif it is valid or False if it is not. The second, a main() function, should get input from the user, call the validator function and print “This is a valid variable name” or “This is not a valid variable name.”

Users should repeatedly be prompted to enter another variable name until they enter “done.” Here is a demo of the program.

Enter your Python variable name: high_temp
This is a valid variable name.

Enter your Python variable name or "done": highTemp2
This is a valid variable name.

Enter your Python variable name or "done": 2_high_temp
This is not a valid variable name.

Enter your Python variable name or "done": done

Note: Python keywords are also not valid variable names but you do not need to test for these.

Part B

Numerology considers the special relationship between a number and one or more coinciding events. It has been used throughout human history as a way to attach meaning to a name, object, or event using mathematics. Numerology is considered a “pseudoscience” since it has no basis in observable phenomena. Nevertheless, it makes for an interesting programming challenge!

1 2 3 4 5 6 7 8 9
A B C D E F G H I
J K L M N O P Q R
S T U V W X Y Z

Your program should start by asking the user to type in their name and convert all the letters to uppercase. In the preceding table, each letter of the alphabet has a corresponding number. Using this table, add up the total of all the number values in the person’s name to arrive at a single whole number. If a user enters spaces or extra punctuation, you can ignore these characters. For example, for the name “Guido”:

“G” = 7
“U” = 3
“I” = 9
“D” = 4
“O” = 6

7 + 3 + 9 + 4 + 6 = 29

Once you have the sum, the number must be reduced so that it is 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, or 22. In numerology, numbers are reduced by adding together their individual digits. For example, 29 reduced is 2 + 9, or, 11. Using this technique, continue to reduce the total until it is a number between 1 and 9, 11, or 22. Hint: You will need to convert the total to a string to iterate over it and convert its digits back to integers before you add them.

Having arrived at the numerology for the name entered, we can now provide the user with “personality associations” for their particular number. Print out the corresponding text based on the following table.

Number Personality Associations
1 initiating action, pioneering, leading, independent, attaining, individual
2 cooperation, adaptability, consideration of others, partnering, mediating
3 expression, verbalization, socialization, the arts, the joy of living
4 a foundation, order, service, struggle against limits, steady growth
5 expansiveness, visionary, adventure, the constructive use of freedom
6 responsibility, protection, nurturing, community, balance, sympathy
7 analysis, understanding, knowledge, awareness, studious, meditating
8 practical endeavors, status oriented, power seeking, material goals
9 humanitarian, giving nature, selflessness, obligations, creative expression
11 higher spiritual plane, intuitive, illumination, idealist, a dreamer
22 the Master Builder, large endeavors, powerful force, leadership

Your final program should work like this.

Enter your name: Guido

Your personality number is: 11

Your personality associations are:
higher spiritual plane, intuitive, illumination, idealist, a dreamer
Part C

To begin, you will be writing a series of functions that can be used as part of a “secret message encoder” program. Here are the functions you will be writing as well as some sample code to use to test your work.

# function:   add_letters
# input:      a word to scramble (String) and a number of letters (integer)
# processing: adds a number of random letters (A-Z; a-z) after each letter 
#             in the supplied word. for example, if word="cat" and num=1 
#             we could generate any of the following:
#             cZaQtR
#             cwaRts
#             cEaett
# 
#             if word="cat" and num=2 we could generate any of the following:
#             cRtaHFtui
#             cnnaNYtjn
#             czAaAitym
#
# output:     returns the newly generated word
def add_letters(word, num):

# function code goes here!

Sample Program

# define original word
original = "Hello!"

# loop to demonstrate the function
for num in range(1, 5):

    # scramble the word using 'num' extra characters
    scrambled = add_letters(original, num)

    # output
    print("Adding", num, "random characters to", original, ". . .", scrambled)

Sample Output

Adding 1 random characters to Hello! . . . HdeulHlHom!t
Adding 2 random characters to Hello! . . . HTLedklFNljioMH!bi
Adding 3 random characters to Hello! . . . HHHZeZrflqSflzOiosNU!jBk
Adding 4 random characters to Hello! . . . HFtRKeivFllRNlUlGTaooYwoH!JpXL

Hint: You will need to use a loop to generate the required number of random characters and you will (obviously) need to use some random number functions as well. Think about the algorithm before you start coding! Draw out the steps you think you need to take on a piece of paper. For example: “Start with the first character in the source word. Then generate ‘num’ new random characters and concatenate these characters. Then move onto the next character in the source word and repeat the process.”

Once you have written the add_letters function you should begin to work on the next function (remove_letters) which will perform the reverse operation.

# function:   remove_letters
# input:      a word to unscramble (String) and the number of characters to remove (integer)
# processing: the function starts at the first position in the supplied word and keeps it.
#             it then removes "num" characters from the word. the process is repeated again
#             if the word contains additional characters - the next character is kept
#             and "num" more characters are removed.  For example, if word="cZaYtU" and
#             num=1 the function will generate the following:
#        
#             cat (keeping character 0, removing character 1, keeping character 2, removing
#                  character 3, keeping character 4, removing character 5)
#
# output:     returns the newly unscrambed word
def remove_letters(word, num):

# function code goes here!

Sample Program

word1 = "HdeulHlHom!t"
word2 = "HTLedklFNljioMH!bi"
word3 = "HHHZeZrflqSflzOiosNU!jBk"
word4 = "HFtRKeivFllRNlUlGTaooYwoH!JpXL"

unscrambled1 = remove_letters(word1, 1)
print("Removing 1 characer from", word1, ". . .", unscrambled1)

unscrambled2 = remove_letters(word2, 2)
print("Removing 2 characers from", word2, ". . .", unscrambled2)

unscrambled3 = remove_letters(word3, 3)
print("Removing 3 characers from", word3, ". . .", unscrambled3)

unscrambled4 = remove_letters(word4, 4)
print("Removing 4 characers from", word4, ". . .", unscrambled4)

Sample Output

Removing 1 characer from HdeulHlHom!t . . . Hello!
Removing 2 characers from HTLedklFNljioMH!bi . . . Hello!
Removing 3 characers from HHHZeZrflqSflzOiosNU!jBk . . . Hello!
Removing 4 characers from HFtRKeivFllRNlUlGTaooYwoH!JpXL . . . Hello!

Hint: String slicing may make your life a lot easier when writing this function!

Finally, write a function called “shift_characters” that shifts the characters in a word up or down based on their ASCII table position—here’s the IPO (input, processing, output) notation and a sample program:

# function:   shift_characters
# input:      a word (String) and a number of characters to shift (integer)
# processing: shifts each character in the supplied word to another position in the ASCII
#             table. the new position is dictated by the supplied integer.  for example,
#             if word = "apple" and num=1 the newly generated word would be:
#
#             bqqmf
#
#             because we added +1 to each character. if we were to call the function with
#             word = "bqqmf" and num=-1 the newly generated word would be:
#           
#             apple
#
#             because we added -1 to each character, which shifted each character down by
#             one position on the ASCII table.
#
# output:     returns the newly generated word
def shift_characters(word, num):

# function code goes here!

Sample Program

word1 = "apple"

newword1 = shift_characters(word1, 1)
print(word1, "shifted by +1 is", newword1)

unscrambled1 = shift_characters(newword1, -1)
print(newword1, "shifted by -1 is", unscrambled1)


word2 = "Pears are yummy!"

newword2 = shift_characters(word2, 2)
print(word2, "shifted by +2 is", newword2)

unscrambled2 = shift_characters(newword2, -2)
print(newword2, "shifted by -2 is", unscrambled2)

Sample Output

apple shifted by +1 is bqqmf
bqqmf shifted by -1 is apple
Pears are yummy! shifted by +2 is Rgctu"ctg"{woo{#
Rgctu"ctg"{woo{# shifted by -2 is Pears are yummy!

Hint: use the ord() and chr() functions!

Encode/Decode

Finally, you are going to write an “encoder/decoder” program that makes use of your three cryptographic functions. Begin by writing a program that continually asks the user to enter in an option—the user can either (e)ncode a word, (d)ecode a word or (q)uit the program.

If the user chooses to encode a word you should do the following:

  • Ask the user for a positive number between 1 and 5. Reprompt them if necessary.
  • Next, ask them to enter in a phrase that they want to encode.
  • Finally, apply the following algorithm to their word:
    • Add ‘num’ random characters in between each letter of their word (using your add_letters) function
    • Shift the word ‘num’ characters (using your shift_characters function)

If the user chooses to decode a word you should do the following:

  • Ask the user for a positive number between 1 and 5. Re-prompt them if necessary.
  • Next, ask them to enter in a phrase that they want to encode.
  • Finally, apply the following algorithm to their word:
    • Subtract ‘num’ random characters in between each letter of their word (using your remove_letters) function
    • Shift the word DOWN by ‘num’ characters (using your shift_characters function)

Here’s a sample running of the program:

(e)ncode, (d)ecode or (q)uit: e
Enter a number between 1 and 5: 1
Enter a phrase to encode: apple
Your encoded word is: boqDqfmsfz

(e)ncode, (d)ecode or (q)uit: d
Enter a number between 1 and 5: 1
Enter a phrase to decode: boqDqfmsfz
Your decoded word is: apple

(e)ncode, (d)ecode or (q)uit: e
Enter a number between 1 and 5: 2
Enter a phrase to encode: Hello, World!! :)
Your encoded word is: JHmgn{nNYnukqFR.Fq"vEYOqYFt[n{lf|Y#mJ#kr"UT<cE+og

(e)ncode, (d)ecode or (q)uit: d
Enter a number between 1 and 5: 2
Enter a phrase to decode: JHmgn{nNYnukqFR.Fq"vEYOqYFt[n{lf|Y#mJ#kr"UT<cE+og
Your decoded word is: Hello, World!! :)

(e)ncode, (d)ecode or (q)uit: q
Sign up to view the entire interaction

Top Answer

View the full answer
partB.py

#dictionary mapping numbers to their associations
associatons = {
'1' : &quot;initiating action, pioneering, leading,
independent, attaining, individual&quot;,
'2' : &quot;cooperation,...

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask a homework question - tutors are online