Question

In: Computer Science

I would like the following code in PYTHON please, there was a previous submission of this...

I would like the following code in PYTHON please, there was a previous submission of this question but I am trying to have the file save as a text file and not a json. Any help would be greatly appreciated.

Create an application that does the following:

The user will enter product codes and product numbers from the keyboard as strings. The product code must consist of four capital letters. The product number can only consist of numeric characters, but the first character must be a zero. If data is entered that doesn't fit the above characteristics, prompt the user to try again.

Once the user enters data in the correct format, add the data to a Dictionary object. The product code should be the key and the product number should be the value.

The above occurs in a loop, so the user can enter multiple code/number values into the Dictionary object. Make sure your loop has a procedure to end at the user's request.

Once the user has finished putting data into the Dictionary object, serialize the Dictionary object to file. The user should be able to provide a file name for the serialized Dictionary object.

Example of the output of a run of the program:

Enter a product code (four capital letters): ABCD
Enter a product number (a number with a leading 0): 012345
Do you wish to continue? Enter y/n: y
Enter a product code (four capital letters): aDDE
That was not a correct product code, please try again: ADDE
Enter a product number (a number with a leading 0): 01234A
That was not a correct product number, please try again: 012340
Do you wish to continue? Enter y/n: n
Please enter a file name: test.dat
File sa
ved

After running the above code, a file named test.dat should have been created that contains a single Dictionary object containing following key/value pairs:

ABCD, 012345
ADDE, 012340

Solutions

Expert Solution

Program Code Screenshot

Sample Output

Program Code to Copy

d = {}
while True:
#Prompt for product code
code = input('Enter a product code (four capital letters): ')
#Keep prompting until valid code is entered
while (len(code) is not 4) or not code.isupper():
code = input('That was not a correct product code, please try again: ')
#Prompt product number
num = input('Enter a product number (a number with a leading 0): ')
#Keep prompting util a valid number is entered
while (not num.isdecimal()) or (num[0] is not '0'):
num = input('That was not a correct product number, please try again: ')
#Store in dictionary
d[code] = num
#Prompt if user wants to add another product
ch = input('Do you wish to continue? Enter y/n: ')
if ch is not 'y':
break

#Open file
name = input('Please enter a file name: ')
f= open(name,'w')
#Write all dictionary items to file
for key,val in d.items():
f.write(key+','+val+'\n')
f.flush()
f.close()
print('File saved')

test.dat


Related Solutions

1. Please program the following in Python 3 code. 2. Please share your code. 3. Please...
1. Please program the following in Python 3 code. 2. Please share your code. 3. Please show all outputs. Instructions: Run Python code  List as Stack  and verify the following calculations; submit screen shots in a single file. Postfix Expression                Result 4 5 7 2 + - * = -16 3 4 + 2  * 7 / = 2 5 7 + 6 2 -  * = 48 4 2 3 5 1 - + * + = 18   List as Stack Code: """...
I have the following python code. I get the following message when running it using a...
I have the following python code. I get the following message when running it using a test script which I cannot send here: while textstring[iterator].isspace(): # loop until we get other than space character IndexError: string index out of range. Please find out why and correct the code. def createWords(textstrings): createdWords = [] # empty list for textstring in textstrings: # iterate through each string in trxtstrings iterator = 0 begin = iterator # new begin variable while (iterator <...
this is a python code that i need to covert to C++ code...is this possible? if...
this is a python code that i need to covert to C++ code...is this possible? if so, can you please convert this pythin code to C++? def main(): endProgram = 'no' print while endProgram == 'no': print # declare variables notGreenCost = [0] * 12 goneGreenCost = [0] * 12 savings = [0] * 12 months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] getNotGreen(notGreenCost, months) getGoneGreen(goneGreenCost, months) energySaved(notGreenCost, goneGreenCost, savings) displayInfo(notGreenCost, goneGreenCost, savings, months)...
PYTHON PLEASE!! ALSO: if you can include an explanation of what the code means that would...
PYTHON PLEASE!! ALSO: if you can include an explanation of what the code means that would be appreciated 8.19 LAB*: Program: Soccer team roster (Dictionaries) This program will store roster and rating information for a soccer team. Coaches rate players during tryouts to ensure a balanced team. (1) Prompt the user to input five pairs of numbers: A player's jersey number (0 - 99) and the player's rating (1 - 9). Store the jersey numbers and the ratings in a...
I would like to compare my current code with that of someone else to be sure...
I would like to compare my current code with that of someone else to be sure I did this assignment correctly. My main concern is with Problems 2 and 4. I sometimes struggle with testing the methods I have created. If you can, please answer all of the questions so that I may reference them if I am still lost on Problems 2 and 4. Each problem requires the one before it and so seeing the entire assignment as opposed...
can you please create the code program in PYTHON for me. i want to create array...
can you please create the code program in PYTHON for me. i want to create array matrix Nx1 (N is multiple of 4 and start from 16), and matrix has the value of elements like this: if N = 16, matrix is [ 4 4 4 4 -4 -4 -4 -4 4 4 4 4 -4 -4 -4 -4] if N = 64, matrix is [8 8 8 8 8 8 8 8 -8 -8 -8 -8 -8 -8 -8...
given the following questions, i would like to ... Question Given the following questions, I would...
given the following questions, i would like to ... Question Given the following questions, I would like to know the answers to c, d, and e. located at the bottom (due to 4 answers per post limit). Thanks! Platinum (Pt) is used in part of your car's catalytic converter to reduce carbon monoxide emissions by the following chemical reaction: 2CO(g) + O2(g) Pt 2CO2(g) ---> 1) A matrix contains 7.1mg Pt/g of matrix. What is the %Pt in the matrix...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the functions defined below. You are expected to re-use these functions in implementing other functions in the file. Include a triple-quoted string at the bottom displaying your output. Here is the starter outline for the homework: a. def count_character(text, char): """ Count the number of times a character occurs in some text. Do not use the count() method. """ return 0 b. def count_sentences(text): """...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the functions defined below. You are expected to re-use these functions in implementing other functions in the file. Include a triple-quoted string at the bottom displaying your output. Here is the starter outline for the homework: g. def big_words(text, min_length=10): """ Return a list of big words whose length is at least min_length """ return [] h. def common_words(text, min_frequency=10): """ Return words occurring at...
Please write in Python code Write a program that stores the following data in a tuple:...
Please write in Python code Write a program that stores the following data in a tuple: 54,76,32,14,29,12,64,97,50,86,43,12 The program needs to display a menu to the user, with the following 4 options: 1 – Display minimum 2 – Display maximum 3 – Display total 4 – Display average 5 – Quit Make your program loop back to this menu until the user chooses option 5. Write code for all 4 other menu choices
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT