35.2k views
1 vote
Write a program that creates a file and asks the user to type 3 different names and their weight and save them in the file.

User Joe Pym
by
7.8k points

1 Answer

5 votes

A program that creates a file and asks the user to type 3 different names and their weight.

def create_and_save_file():

# Get the file name from the user

file_name = input("Enter the name of the file (e.g., data.txt): ")

# Open the file in write mode

with open(file_name, 'w') as file:

# Ask the user to input three names and weights

for i in range(3):

name = input("Enter a name: ")

weight = input("Enter the weight for {} (in kg): ".format(name))

# Write the name and weight to the file

file.write("Name: {}\tWeight: {} kg\\".format(name, weight))

print("Data has been saved to the file '{}'.".format(file_name))

# Run the program

create_and_save_file()

User Seamus Barrett
by
8.8k points

No related questions found