Question

In: Computer Science

python programming • Appropriate and well constructed while and/or for loops (as necessary). • Appropriate if,...

python programming

• Appropriate and well constructed while and/or for loops (as necessary). • Appropriate if, if-else, if-elif-else statements (as necessary). • The use of the ord() and chr() functions (as necessary). • The following three functions (refer to stage 6 for description): o display_details() o get_menu_choice() o get_offset() • Output that strictly adheres to the assignment specifications. If you are not sure about these details, you should check with the ‘Sample Output – Part II’ provided at the end of this document. • Good programming practice: o Consistent commenting, layout and indentation. You are to provide comments to describe: your details, program description, all variable definitions, and significant sections of code. o Meaningful variable names. • Your solutions MAY make use of the following built-in functions and methods: o Any of the Python built-in functions… specifically, you may find you may need the following: int(), input(), print(), range(), ord(),and chr(). • Your solutions MAY ALSO make use of the following: o Concatenation (+) operator to create/build new strings. o Access the individual elements in a string with an index (one element only). i.e. string_name[index]. o Your own (user-defined) functions (in addition to the three functions listed above). Your solutions MUST NOT use: • break, or continue statements in your solution. Do not use the quit() or exit() functions or the break or return statements (or any other techniques) as a way to break out of loops. Doing so will result in a significant mark deduction.

Sample output

*** Menu ***

1. Encrypt string

2. Decrypt string

3. Brute force decryption

4. Quit

What would you like to do [1,2,3,4]? 1

Please enter string to encrypt: Elvis has left the building!

Please enter offset value (1 to 94): 3

Encrypted string: Hoylv#kdv#ohiw#wkh#exloglqj$

*** Menu ***

1. Encrypt string

2. Decrypt string

3. Brute force decryption

4. Quit What would you like to do [1,2,3,4]? 2

Please enter string to decrypt: Hoylv#kdv#ohiw#wkh#exloglqj$ Please enter offset value (1 to 94): 3

Decrypted string: Elvis has left the building!

*** Menu ***

1. Encrypt string

2. Decrypt string

3. Brute force decryption

4. Quit What would you like to do [1,2,3,4]? 3

Please enter string to decrypt: Hoylv#kdv#ohiw#wkh#exloglqj$

Offset: 1 = Decrypted string: Gnxku"jcu"nghv"vjg"dwknfkpi# Offset: 2 = Decrypted string: Fmwjt!ibt!mfgu!uif!cvjmejoh" Offset: 3 = Decrypted string: Elvis has left the building! Offset: 4 = Decrypted string: Dkuhr~g`r~kdes~sgd~athkchmf Offset: 5 = Decrypted string: Cjtgq}f_q}jcdr}rfc}`sgjbgle~ Offset: 6 = Decrypted string: Bisfp|e^p|ibcq|qeb|_rfiafkd} Offset: 7 = Decrypted string: Ahreo{d]o{habp{pda{^qeh`ejc| Offset: 8 = Decrypted string: @gqdnzc\nzg`aozoc`z]pdg_dib{ Offset: 9 = Decrypted string: ?fpcmyb[myf_`nynb_y\ocf^chaz Offset: 10 = Decrypted string: >eoblxaZlxe^_mxma^x[nbe]bg`y Offset: 11 = Decrypted string: =dnakw`Ykwd]^lwl`]wZmad\af_x Offset: 12 = Decrypted string: P\IBCQ\QEB\?RFIAFKD] Offset: 39 = Decrypted string: !HREO[D=O[HABP[PDA[>QEH@EJC\ Offset: 40 = Decrypted string: GQDNZCCHAZ Offset: 42 = Decrypted string: }EOBLXA:LXE>?MXMA>X;NBE=BG@Y Offset: 43 = Decrypted string: |DNAKW@9KWD=>LWL@=W:MADW Offset: 45 = Decrypted string: zBL?IU>7IUB;;U8K?B:?D=V Offset: 46 = Decrypted string: yAK>HT=6HTA:;ITI=:T7J>A9>CH;EQ:3EQ>78FQF:7Q4G;>6;@9R Offset: 50 = Decrypted string: u=G:DP92DP=67EPE96P3F:=5:?8Q Offset: 51 = Decrypted string: t7P Offset: 52 = Decrypted string: s;E8BN70BN;45CNC74N1D8;38=6O Offset: 53 = Decrypted string: r:D7AM6/AM:34BMB63M0C7:27<5N Offset: 54 = Decrypted string: q9C6@L5.@L923ALA52L/B6916;4M Offset: 55 = Decrypted string: p8B5?K4-?K812@[email protected]:3L Offset: 56 = Decrypted string: o7A4>J3,>J701?J?30J-@47/492K Offset: 57 = Decrypted string: n6@3=I2+=I6/0>I>2/I,?36.381J Offset: 58 = Decrypted string: m5?2

25-270I Offset: 59 = Decrypted string: l4>1;G0);G4-.' 2>+$%3>3'$>!4(+#(-&? Offset: 69 = Decrypted string: b*4'1=&~1=*#$2=2= 3'*"',%> Offset: 70 = Decrypted string: a)3&0<%}0<)"#1<1%"<~2&)!&+$= Offset: 71 = Decrypted string: `(2%/;$|/;(!"0;0$!;}1%( %*#< Offset: 72 = Decrypted string: _'1$.:#{.:' !/:/# :|0$'~$)"; Offset: 73 = Decrypted string: ^&0#-9"z-9&~ .9."~9{/#&}#(!: Offset: 74 = Decrypted string: ]%/",8!y,8%}~-8-!}8z."%|"' 9 Offset: 75 = Decrypted string: \$.!+7 x+7$|},7, |7y-!${!&~8 Offset: 76 = Decrypted string: [#- *6~w*6#{|+6+~{6x, #z %}7 Offset: 77 = Decrypted string: Z",~)5}v)5"z{*5*}z5w+~"y~$|6 Offset: 78 = Decrypted string: Y!+}(4|u(4!yz)4)|y4v*}!x}#{5 Offset: 79 = Decrypted string: X *|'3{t'3 xy(3({x3u)| w|"z4 Offset: 80 = Decrypted string: W~){&2zs&2~wx'2'zw2t({~v{!y3 Offset: 81 = Decrypted string: V}(z%1yr%1}vw&1&yv1s'z}uz x2 Offset: 82 = Decrypted string: U|'y$0xq$0|uv%0%xu0r&y|ty~w1 Offset: 83 = Decrypted string: T{&x#/wp#/{tu$/$wt/q%x{sx}v0 Offset: 84 = Decrypted string: Sz%w".vo".zst#.#vs.p$wzrw|u/ Offset: 85 = Decrypted string: Ry$v!-un!-yrs"-"ur-o#vyqv{t. Offset: 86 = Decrypted string: Qx#u ,tm ,xqr!,!tq,n"uxpuzs- Offset: 87 = Decrypted string: Pw"t~+sl~+wpq + sp+m!twotyr, Offset: 88 = Decrypted string: Ov!s}*rk}*vop~*~ro*l svnsxq+ Offset: 89 = Decrypted string: Nu r|)qj|)uno})}qn)k~rumrwp* Offset: 90 = Decrypted string: Mt~q{(pi{(tmn|(|pm(j}qtlqvo) Offset: 91 = Decrypted string: Ls}pz'ohz'slm{'{ol'i|pskpun( Offset: 92 = Decrypted string: Kr|oy&ngy&rklz&znk&h{orjotm' Offset: 93 = Decrypted string: Jq{nx%mfx%qjky%ymj%gznqinsl& Offset: 94 = Decrypted string: Ipzmw$lew$pijx$xli$fymphmrk%

*** Menu ***

1. Encrypt string

2. Decrypt string

3. Brute force decryption

4. Quit What would you like to do [1,2,3,4]? 4 Goodbye.

Note: Your program must work with the printable ASCII character set. That is, all the characters from ASCII 32 (Space) to ASCII 126 (~). When the offset points to a character beyond 126 it should wrap around to the beginning of the set.

Solutions

Expert Solution

# display menu options
def display_details():
    print('***Menu***')
    print('1. Encrypt string')
    print('2. Decrypt string')
    print('3. Brute force decryption')
    print('4. Quit')


# returns a number from 1 to 4 indicating menu option
def get_menu_choice():
    # prompt user for input
    choice = input('What would you like to do [1,2,3,4]? ')
    # validate user input
    if choice.isdigit():
        option = int(choice)
        # check if option is in valid range
        if (option < 5) and (option > 0):
            # return valid input
            return option
        else:
            # option is not in valid range
            # print error and try again
            print('Invalid Input!')
            return get_menu_choice()
    else:
        # non digit character received
        # print error message
        print('Invalid Input!')
        # try again for valid input
        return get_menu_choice()


# returns a number from 1 to 94 indicating offset value
def get_offset():
    # prompt user for input
    choice = input('Please enter offset value (1 to 94): ')
    # validate user input
    if choice.isdigit():
        option = int(choice)
        # check if option is in valid range
        if (option < 95) and (option > 0):
            # return valid input
            return option
        else:
            # option is not in valid range
            # print error and try again
            print('Invalid Input!')
            return get_offset()
    else:
        # non digit character received
        # print error message
        print('Invalid Input!')
        # try again for valid input
        return get_offset()


# Encrypts a given string with given offset
# increase each character value in the string to given offset
# prints resulting string
def encrypt(message, offset):
    # create a string to print
    encoded = ''
    # loop through message and increase its character values
    for i in range(len(message)):
        # get integer value of individual character
        value = ord(message[i])
        # increase value to offset
        value = value + offset
        # check if value is in printable range
        if value > 126:
            # wrap value at start
            value = value - 93
        # get character of the new value
        character = chr(value)
        # build string character by character
        encoded = encoded + character
    # print encoded message
    print('Encrypted string: ' + encoded)


# Decrypts a given string with given offset
# decrease each character value in the string to given offset
# prints resulting string
def decrypt(message, offset):
    # create a string to print
    decoded = ''
    # loop through message and decrease its character values
    for i in range(len(message)):
        # get integer value of individual character
        value = ord(message[i])
        # decrease value to offset
        value = value - offset
        # check if value is in printable range
        if value < 32:
            # wrap value at end
            value = value + 95
        # get character of the new value
        character = chr(value)
        # build string character by character
        decoded = decoded + character
    # print decoded message
    print('Decrypted string: ' + decoded)


# main function to run the program
def main():
    # prompt user for input till Quit is selected
    option = 0  # initialize value to enter in loop
    while option != 4:
        display_details()
        option = get_menu_choice()
        # check user selected option
        if option == 1:
            # user selected to Encrypt string
            # ask user for a string to encrypt
            message = input('Please enter string to encrypt: ')
            encrypt(message, get_offset())
        elif option == 2:
            # user selected to Decrypt string
            # ask user for a string to decrypt
            message = input('Please enter string to decrypt: ')
            decrypt(message, get_offset())
        elif option == 3:
            # user selected to brute force decryption
            # ask user for a string to decrypt
            message = input('Please enter string to decrypt: ')
            # try every offset from 1 to 94 to decrypt the message
            for i in range(94):
                # print each offset tried
                print('Offset:', end=' ')
                print(i+1, end=' = ')
                decrypt(message, i+1)
        else:
            # when option is Quit print end message
            print('Goodbye')


# run the program
main()

let me know if you have any doubts or problem running the code.


Related Solutions

Code in Python. You can only use while loops NOT for loops. Program 1: cost_living In...
Code in Python. You can only use while loops NOT for loops. Program 1: cost_living In 2020 the average cost of living/month (excluding housing) for a family of 4 in Pittsburgh was $3850 per month. Write a program to print the first year in which the cost of living/month is over $4450 given that it will rise at a rate of 2.1% per year. (Note:  this program requires no input). Program 2: discount A discount store is having a sale where...
Python Exercises = Sentinel Values and While Loops #Exercise 1 #Write a while loop with a...
Python Exercises = Sentinel Values and While Loops #Exercise 1 #Write a while loop with a sentinel value #This while loop ends when the user enters a 1, the sentinel value #Assign the number 8 to a variable which will serve as the sentinel value #Condition: while variable is not equal to 1 #Action: display the number assigned to the variable #Use an input statement (no prompt) to ask the user for a number and assign this number to the...
This is for Python programming, and I am trying to use 2 for loops to ask...
This is for Python programming, and I am trying to use 2 for loops to ask a slaesperson how many of 5 different items they sold, then the program is to calculate the total dollar amount sold. Formatting and all that aside, I am having an issue with the loops not stepping through the 2 lists at the same time. The first loop is taking all 5 entered quantities times the price of the first item, and then all 5...
Python Programming This assignment will give you practice with interactive programs, if/else statements, collections, loops and...
Python Programming This assignment will give you practice with interactive programs, if/else statements, collections, loops and functions. Problem Description A small car yard dealing in second hand cars needs an application to keep records of cars in stock. Details of each car shall include registration(rego), model, color, price paid for the car (i.e. purchase price) and selling price. Selling price is calculated as purchased price plus mark-up of 30%. For example, Toyota Corolla bought for $20,000 will have the selling...
Intro to Python! 1. Using while loops, write a program that prints multiples of 5 which...
Intro to Python! 1. Using while loops, write a program that prints multiples of 5 which are less than 100. Your loop initially starts from 0. 2. Consider B to be a list of alphabetically ordered strings. Using while loops, write a program that only prints words which start with letter A. B = [Alex, Airplane, Alpha, Australia, Ben, Book, Bank, Circuit, Count, Dark] 3. Using while loop, create a program that only prints first 18 multiples of 7.Hint: Your...
Using Python programming language, write a LONG piece of code that utilizes the do while function...
Using Python programming language, write a LONG piece of code that utilizes the do while function and the switch statement, please do not make It short, thank you.
2. What loops run while in a true condition?
  In your own words write an example of a loop that you perform in your day to day life   Sample answer - If(“want to make money” –eq “true”){   Goto-work   Now answer the following questions   2. What loops run while in a true condition?   3. What loop runs while in a false condition?   4. What PowerShell loop sets the variable, the condition, and the iteration of the variable all in one?   5. Describe...
how would i change the for loops to while loops in the code below #include<stdio.h> #include<stdlib.h>...
how would i change the for loops to while loops in the code below #include<stdio.h> #include<stdlib.h> int main() { int seed; // Taking seed value as input from the user printf("Enter a seed value (0 to quit): \n"); scanf("%d", &seed); // Running the loop until user enters 0 to quit // count array will count frequency of 0 , 1 , 2 ,3 int count[4]; for (int i = 0; i < 4; i++) count[i] = 0; while (seed !=...
Python Programming Please! #Name: #Date: #Random number, loop while true #ask user for number. Check to...
Python Programming Please! #Name: #Date: #Random number, loop while true #ask user for number. Check to see if the value is a number between 1 and 10 #if number is too high or too low, tell user, if they guessed it break out of loop Display "Welcome to my Guess the number program!" random mynumber count=1 while True try Display "Guess a number between 1 and 10"   Get guess while guess<1 or guess>10 Display "Guess a number between 1 and...
Read chapter 3 from our text, Murach's Python Programming, focusing on coding the iteration structure (while...
Read chapter 3 from our text, Murach's Python Programming, focusing on coding the iteration structure (while statement). Then code Python statements for the pseudocode under a "Payment Schedule" shown below. Save these Python statements in a text file named with your first name, last name, the digits 06, and extension .py (e.g. joelMurach06.py). To maximize points earned, first test this file with Python IDLE to ensure it is error free, then attach the python file to an email sent to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT