0

I'm new to python (a couple weeks) and the first project i'm working on is creating an application to more easily study Russian verb conjugations. Right now I'm working on the functions of the program itself, once I have that I will dive into the GUI.

The idea is that when the user loads the program they can choose to create/modify sets or study them. From there they either choose which study set to study or begin creating a study set. So far the only function I have is one that returns a random verb and conjugate form (this part of my code works, just giving some context).

import random
import json

test_verbs = [{
    'verb': 'изучать',
    'defintion': 'to study in depth',
    'я': 'изучаю',
    'ты': 'изучаешь',
    'он': 'изучает',
    'вы': 'изучаете',
    'мы': 'изучаем',
    'они': 'изучают',
    'informal imperative': 'изучай',
    'formal_imperative': 'изучайте',    
},
{'verb': 'говорить',
'definition': 'to talk',
'я': 'говорю',
'ты': 'говоришь',
'он': 'говорит',
'вы': 'говорите',
'мы': 'говорим',
'они': 'говорят',
'informal imperative': 'говори',
'formal imperative': 'говорите'}]

#function to randomly generate a verb and a pronoun, conjugation pair
def random_conjugation(verb_list):

        random_verb = random.choice(verb_list)
        print( f"the verb is {random_verb['verb']}" )

        conjugation_dict_fields = ['я', 'ты', 'он', 'вы', 'мы', 'они']
        random_form = random.choice( conjugation_dict_fields )

        print( f"What is the {random_form} form of the verb?" )
        print( random_verb[random_form] )

#function test
random_conjugation(test_verbs)

The problem I'm trying to tackle right now is getting my lists into a .json file so the user created lists can be saved.

My initial attempt was this, but it clearly did not work:

def create_new_list(list_name):
    list_name = []
    filename = f'{list_name}.json'
    print("type 'q' to stop adding entries to the list")
    while True:
        verb = input("Type a new verb:  ")
        list_name.append(verb)
        if verb == 'q':
            break
    with open(filename, 'w') as f:
        json.dump(list_name, f)

I don't have my list formatted in a way that matches my verb dictionary storage structure, but i figured it would be easier for you guys to help me without having a complicated dictionary and Russian pronouns get in the way...

I know my solution does not work, I clearly am missing a core concept here, but it was my best attempt. I thought it would be a good solution because the user creates the name of the new list, and that list name also becomes the name_of_the_file.json

Maybe there's a simple solution here, or maybe I'm going about this in the completely wrong way. Any help is appreciated here.

  • 1
    You should break out of the loop *before* appending the input to the list. – Barmar Jan 06 '20 at 22:39
  • wouldn't that mean I could only add one item to my list? Regardless, the main problem I'm having is that apparently I can't define a list with ```function(list_name): list_name = []``` Thanks! – coding_guy69 Jan 06 '20 at 22:43
  • What does _it clearly did not work_ mean, exactly? – AMC Jan 06 '20 at 22:45
  • When you do `list_name = []` you're discarding the old value of `list_name` and setting the variable to an empty list. – Barmar Jan 06 '20 at 22:46
  • I'm not sure what the initial code with `random_conjugation` has to do with the function at the end that you're asking about. – Barmar Jan 06 '20 at 22:47
  • I guess it just felt like bad code. Figured it would look really ugly to you guys. New to this. But thank you, now it makes sense why ```list_name = [] ``` does not work – coding_guy69 Jan 06 '20 at 22:51

2 Answers2

1

Don't use the same list_name variable for the filename and the list of verbs.

def create_new_list(list_name):
    filename = f'{list_name}.json'
    verblist = []
    print("type 'q' to stop adding entries to the list")
    while True:
        verb = input("Type a new verb:  ")
        if verb == 'q':
            break
        verblist.append(verb)

    with open(filename, 'w') as f:
        json.dump(verblist, f)

If you want to set some other variable outside the function to this list, you should return the list from the function, and then assign the variable.

def create_new_list(list_name):
    filename = f'{list_name}.json'
    verblist = []
    print("type 'q' to stop adding entries to the list")
    while True:
        verb = input("Type a new verb:  ")
        if verb == 'q':
            break
        verblist.append(verb)

    with open(filename, 'w') as f:
        json.dump(verblist, f)

    return verblist

list_of_verbs = create_new_list("verbs")
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You were overwriting the variable list_name, I usually prefer to avoid while True so here's how I'd write it

def create_new_list(list_name):
    filename = f'{list_name}.json'
    verb_list = []
    print("type 'q' to stop adding entries to the list")
    verb = input("Type a new verb:  ")
    while verb != 'q':
        verb_list.append(verb)
        verb = input("Type a new verb:  ")

    with open(filename, 'w') as f:
        json.dump(verb_list, f)

I'd also check if the file exists or not to avoid overwriting if it already exists

Also check out this answer for nicer formatting https://stackoverflow.com/a/12309296

Jimmar
  • 4,194
  • 2
  • 28
  • 43
  • 1
    I usually recommend the `while True:` style, so you don't have to have two identical `verb = input(...)` lines. – Barmar Jan 06 '20 at 22:59
  • @Barmar it's more of a style thing, I'd prefer it this way so I don't run into unexpected infinite loops and avoid `break/continue` – Jimmar Jan 06 '20 at 23:01