Success! (sort of)

And here it is, it only took me 7 hours. (stop laughing!)

It is finally here, my first Python project. The fact that it only works once before it resets itself is by-the-by. I have made it!

I had a lot of problems, but the main one was making dictionary additions persistent. I read up on a module called shelves that seemed like it would be the perfect thing, but could I get it to work, of course not! So after that I tried just a basic .txt file with entries on one line, which did work, but only for names. I had originally had ‘Dan 23’ on one line, and ‘Peter 24’ on the next, and while I could get .splitlines() working, I couldn’t get .split() working, so the programme kept telling me that I wasn’t in the file even though I was.

The (kind of) finished code is at the bottom; I am going to keep at this one until I find a solution that I am both happy with, and understand.

To answer the question in my previous post, about line 8, which is now line 9, it was a case of something being far more simple than I expected. Take a look, it’s super easy. That said, I did see come incredibly convoluted answers to that question on the internet, so, you know, swings and roundabouts.


the_book = {'Dan': 23, 'Peter': 24}

def check_name_in_book():
    user_name = get_user_name()
    if user_name in the_book:
        print('Your name is in the book, ', user_name, ' age: ', the_book[user_name])
        print('Your name is not in the book, ', user_name, '. Would you like it to be?')
        not_in_book_answer = input ('')
        n_i_b_a_l = not_in_book_answer.lower()
        if n_i_b_a_l[0] == 'n':
            user_age = get_age(user_name)
            append_the_book(user_name, user_age)
def get_user_name():
    print('Please enter your name:')
    user_name = input('')
    return user_name

def get_age(user_name):
    print('Hi ', user_name, '. Please enter your age:')
    user_age = input ('')
    print('Thanks ', user_name, ', who is ', user_age, '. You will be added to the book.')   
    return user_age

def append_the_book(user_name, user_age):
    the_book[user_name] = user_age
    return the_book


Author: Dan

Share This Post On