-3

a program to store information about a single user. The program should include a function that asks the user for their name, age, course, and home town and stores this in memory. It should also have a function that will write the information entered in a file. Use exception handling to protect the data entry and the file operations

really stuck on this any help would be great

matt
  • 11
  • 2
  • Please put examples of what you have tried in the question. – NendoTaka Nov 21 '17 at 17:37
  • Possible duplicate of [Correct way to write line to file?](https://stackoverflow.com/questions/6159900/correct-way-to-write-line-to-file) – Davy M Nov 21 '17 at 17:39

1 Answers1

1
name=raw_input("Enter name :")
surname=raw_input("Enter surname :")

n=None
while n is None:
    age=raw_input("Enter age :")
    try:
        n = int(age)
    except ValueError:
        print "Not a number."

course=raw_input("Enter course :")
hometown=raw_input("Enter hometown :")



with open("workfile","w") as f:
    f.write('Name : ' + name + '\n')
    f.write('Surname : ' + surname + '\n')
    f.write('Age : ' + str(age) + '\n')
    f.write('Course : ' + course + '\n')
    f.write('Hometown : ' + hometown + '\n')


f.close()

for exception handling in file I/O see What is a good way to handle exceptions when trying to read a file in python?

ralf htp
  • 9,149
  • 4
  • 22
  • 34