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

Hi, I would like the following python code rewritten in a different way/structure etc. I got...
Hi, I would like the following python code rewritten in a different way/structure etc. I got it from a question I posted but am worried fellow classmates will also be using it so am covering bases. her it is. #threeUniqueSongs.py #Simulation of professor listening to 3 songs out of playlist of 4 songs 10000 times sampling with replacement possible import random import math #Here playlist is the list of 4 songs i.e. "Eastside", "Better Now", "Lucid Dreams", "Harder, Better, Faster,...
what is the solution of this code? Also how would I run it in python to...
what is the solution of this code? Also how would I run it in python to check? q = Queue() q.enqueue(10) q.enqueue(12) q.enqueue(15) for i in range(len(q)): → if len(q) % 2 == 0: → → q.enqueue(q.dequeue()) → else: → → q.dequeue() print(q)
As in previous labs, please write the Java code for each of the following exercises in...
As in previous labs, please write the Java code for each of the following exercises in BlueJ. For each exercise, write a comment before the code stating the exercise number. Make sure you are using the appropriate indentation and styling conventions Exercise 1 (15 Points) In BlueJ, create a new project called Lab6 In the project create a class called Company Add instance data (fields) for the company name (a String) and the sales for the current period (a double)...
This is in Python: Alter your code for previous Do It Now problem, with the change...
This is in Python: Alter your code for previous Do It Now problem, with the change that the row should only be printed by the second number that the user enters. For example, the user enters two numbers of 5 and 7 for the two input lines in the code, and the following will be printed: 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 =...
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 <...
can you please convert this python code into java? Python code is as shown below: #...
can you please convert this python code into java? Python code is as shown below: # recursive function def row_puzzle_rec(row, pos, visited):    # if the element at the current position is 0 we have reached our goal    if row[pos] == 0:        possible = True    else:        # make a copy of the visited array        visited = visited[:]        # if the element at the current position has been already visited then...
I am using NetBeans IDE Java for coding. I would like the code to be commented...
I am using NetBeans IDE Java for coding. I would like the code to be commented for a better understanding. 1. Implement a class Robot that simulates a robot wandering on an infinite plane. The robot is located at a point with integer coordinates and faces north, east, south, or west. Supply methods: public void turnLeft() public void turnRight() public void move() public Point getLocation() public String getDirection() The turnLeft and turnRight methods change the direction but not the location....
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)...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT