Question: FIX THE ERROR PLEASE: Using the input text file: name:James age:20 shirt:polo color:blue shirt:tshirt color:purple shirt:sleeveless color:orange # Open the input and output files with
FIX THE ERROR PLEASE:
Using the input text file:
name:James age:20
shirt:polo color:blue
shirt:tshirt color:purple
shirt:sleeveless color:orange
# Open the input and output files
with open('input.txt', 'r') as input_file, open('output.txt', 'w') as output_file:
# Initialize a dictionary to store the person's data
person = {'name': '', 'age': '', 'shirts': []}
# Loop over each line in the input file
for line in input_file:
# Split the line into key and value
key, value = line.strip().split(':')
# Store the value in the person dictionary
if key == 'name':
person['name'] = value.strip()
elif key == 'age':
person['age'] = int(value.strip())
elif key == 'shirt':
person['shirts'].append(value.strip())
# If the person is 20 years old, write the output to the output file
if person['age'] == 20:
output_file.write(f"name: {person['name']} ")
output_file.write("shirts: ")
# Loop over the person's shirts and write them to the output file
for i, shirt in enumerate(person['shirts']):
output_file.write(f"{i+1}. {shirt} ")
# Loop over the person's shirts again and write their colors to the output file
for shirt in person['shirts']:
output_file.write(f" {person['name']} has the shirt: {shirt} ")
color = input(f"what is the color of {shirt}?: ").strip()
output_file.write(f"the color is: {color} ")
RESULTS IN THE ERROR:
key, value = line.strip().split(':') ^^^^^^^^^^ ValueError: not enough values to unpack (expected 2, got 1)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
