Reputation: 13
I work in Python 3. I want to make a contacts app with I/O. I want to re-run the code from specific line.
What is the best way to do that?
#Contact
Contact=input("Enter Name You Want To Save")
f=open(r'C:\Users\Adham\Desktop\we.txt',"w")
f.writelines(Contact)
f.close()
cont=input("write a for another name and l to load the previous save or close the program ")
if cont=="a":
Contact=input("Enter Name You Want To Save")
f=open(r'C:\Users\Adham\Desktop\we.txt',"w")
f.writelines(Contact)
f.close()
elif cont=="l":
f=open(r'C:\Users\Adham\Desktop\we.txt',"r")
print(f.readlines())
else:
print('sorry you made something wrong')
Upvotes: 1
Views: 842
Reputation: 10086
If you want to use the same code (slightly modified) multiple times, write a function
and call this function
every time, you need it.
A basic Python tutorial can be found here as an example: http://www.tutorialspoint.com/python/python_functions.htm
Upvotes: 2