project 2

docx

School

Central Piedmont Community College *

*We aren’t endorsed by this school

Course

121

Subject

Computer Science

Date

Dec 6, 2023

Type

docx

Pages

4

Uploaded by PresidentHackerMonkey2290

Report
# 4/16/2023 # Project 2 # Write a program that will generate a bill for sending a telegram and # give the user the opportunity to translate a message into Morse code. # Customers are charged at a rate of $1.50 for blocks of 5 words and $0.50 for single words. # 1-Add an option to the menu to offer the user a choice of processing a data file. Add another option to quit # the program. The menu should be the first thing displayed when your program begins executing. # The menu format should match the one in the Sample Output # 2-Allow the user to continue making menu choices until they choose the option to quit. Always display the menu # before prompting the user for a menu option. Properly handle an invalid menu choice. The user should have an # unlimited number of chances to enter a valid menu choice. # 3-Validate the user’s input for number of words sent to not accept values less than 1. Allow the user an # unlimited number of chances to enter a valid number for words sent. # 4-Modify the code you wrote in Project 2 to translate a single letter into a Morse code. Your code # should now be able to efficiently translate a complete message of any length into Morse code. # All white spaces in the message should be translated into 3 corresponding white spaces in Morse code. # 5-Add functionality to create and display telegram bills by reading input from the TelegramData.txt text file. # When the user selects the option to process a data file, telegram bills should be created and displayed for every # record in the file without the user needing to do anything else. See the Sample Output for what the bill must contain. # The TelegramData.txt file contains 5 records, but your program must be able to process an identically formatted file # with any number of records without counting the records. # Function to translate a letter to Morse code def translate_to_morse ( letter ): morse_dict = { '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' : '--..' } return morse_dict. get ( letter . upper (), '' ) # Function to input - getting data for client def process_telegram_bill (): while True : customer_name = input ( "Enter customer's first and last name (or 'q' to quit): " ) # - Checking if input is q then exit the program
if customer_name == 'q' : break street_address = input ( "Please enter your address: " ) city = input ( "Please enter your city: " ) city = city. capitalize () state = input ( "Please enter your state (ex. NC for North Carolina): " ) state = state. upper () zip_code = input ( "Please enter your Zip code: " ) num_words = input ( "Please enter how many words in your message to send: " ) # Validate number of words while True : try : num_words = int (num_words) if num_words < 1 : raise ValueError break except ValueError : num_words = input ( "Invalid input. Please enter the number of words[at least 1 or greater]: " ) # Count number of words and calculate cost cost_block = num_words // 5 cost_single = num_words % 5 amount_billed = (cost_block * 1.5 ) + (cost_single * 0.5 ) # Print the bill print () print (customer_name) print (street_address) print ( f' { city } , { state } { zip_code } ' ) print ( f'Amount Owed: $ { amount_billed : .2f } ' ) print () # Function - input - translating text to morse code def translate_message (): message = input ( 'Enter a message: ' ) # Translate each letter and print the result result = '' for letter in message: if letter. isalpha (): result += translate_to_morse (letter. upper ()) elif letter == '' : result += '' else : result += letter + '' # Output - printing out the morse code print ( f"Translation: { result }\n " ) # Function - processing data from file MorseCodeDataFile.txt and outputting it to print def process_data_file (): print ( 'Processing Morse code data file...' )
# Open the data file # Read data from file with open ( 'MorseCodeDataFile.txt' , 'r' ) as file: # Loop through each line in the file data = file. read () lines = data. strip (). split ( ' \n ' ) initial = 0 count = 5 for line in range ( len (lines)): # Extract the fields from the line fields = lines[initial:count + 1 ] if initial - 1 == len (lines): break initial += 7 count += 7 # Assign the fields to the variables name, address, city, state, zipcode, words = fields words = float (words) # Count number of words and calculate cost cost_block = words // 5 cost_single = words - (cost_block * 5 ) words_billed = (cost_block * 1.5 ) + (cost_single * 0.5 ) # Output Print the date pulled from MorseCodeDataFile.txt print () print (name) print (address) print ( "{0},{1} {2}" . format (city, state, str (zipcode))) print ( f'Amount Owed: $ { words_billed : .2f } ' ) print () # close - morseCodeDataFile.txt file. close () print ( 'Finished processing Morse code data file.' ) # Displaying program and giving user choice to enter different functions in the program while True : print ( '==========================================' ) print ( 'Welcome to Western Union Telegraph Company' ) print ( '==========================================' ) print ( '1 - Process Telegram Bill' ) print ( '2 - Translate to Morse code' ) print ( '3 - Process a Data File' ) print ( '4 - Quit' ) choice = input ( 'Enter your choice: ' ) # if 1 selected -process telegram function if choice == '1' : process_telegram_bill () # if 2 selected - translate message function elif choice == '2' : translate_message () # if 3 selected - process data elif choice == '3' :
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
process_data_file () # if 4 selected - quit program elif choice == '4' : print ( 'Quitting program...GOOD BYE' ) break # if 1-4 is not select prompt user error message to reenter valid input 1- 4 else : print ( '*****Error - Please enter a number between 1 and 4.*****' )