Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sqlite3 as sql
- """
- create a program that will ask the users name, age, and reddit username. have it tell them the information back, in the format:
- your name is (blank), you are (blank) years old, and your username is (blank)
- for extra credit, have the program log this information in a file to be accessed later.
- """
- db_path = "c1.db"
- con = sql.connect(db_path)
- cur = con.cursor()
- def get_info():
- print "What is your name, friend?"
- name = raw_input("> ")
- print "How old are you?"
- age = raw_input("> ")
- print "What is your reddit username?"
- username = raw_input("> ")
- print "\n","="*72
- return (name,age,username)
- def record(info,write=True):
- cur.execute("create table if not exists challenge1( name text, age text, username text)") #Creates database table if it doesn't already exist.
- if write:
- cur.execute("insert into challenge1 values(?,?,?)",info)
- def read_data():
- try:
- raw_data = cur.execute("select * from challenge1")
- except sql.OperationalError:
- record(info=None,write=False)
- raw_data = cur.execute("select * from challenge1")
- data = []
- for row in raw_data:
- data.append(row)
- return data
- def main():
- while True:
- print "1: Add entry"
- print "2: List entries"
- print "3: Drop table"
- print "4: Exit"
- next = raw_input("> ")
- print "="*72
- if next == '1':
- info = get_info()
- record(info)
- if next == '2':
- data = read_data()
- for listing in data:
- name = listing[0]
- age = listing[1]
- username = listing[2]
- print "your name is '%s', you are '%s' years old, and your username is '%s'." % (name,age,username)
- if listing != data[-1]:
- print ""
- print "="*72
- if next == '3':
- cur.execute("drop table if exists challenge1")
- print "Dropped the table!"
- print "="*72
- if next == 'end' or next == 'break' or next == 'exit' or next == '4':
- break
- con.commit()
- con.close()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement