Python Forum
Writing to a .py file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing to a .py file
#1
import time
import sys
from dictionary import spanishDict as x
from dictionary import frenchDict as y

""" Once you go through the menu and choose to add a word to a dictionary and type in the word and its translation"""
"""I am trying to take those words and update a .py file called dictionary so when i open up dictionary.py its updated with the new words """

        
def mainMenu():
    """Creates the Main Menu"""
    print("Main Menu")
    print("1 - Translate a word")
    print("2 - Print current translation dictionaries")
    print("3 - Add a word to a dictionary")
    print("4 - Exit")
    choice=int(input("Please choose a number: "))
    
    
    if choice >4 or choice <1:
        print("Please pick a number 1 - 4 only!")
        choice=input("Choose a number: ")
    if choice == 1:
        choiceOne()
    if choice == 2:
        choiceTwo()
    if choice == 3:
        choiceThree()
    if choice == 4:
        choiceFour()

def returnMenu():
    """Creates an alt Menu to see if the user wants to continue or exit program"""
    print("M = Main Menu")
    print("Q = Quit")
    answer=input("Would you like to return to the main menu or quit: ")
    while answer != "M" and answer != "m" and answer != "Q" and answer != "q":
        answer=input("Please choose between M, Q, m, q: ")
    if answer == "M" or answer == "m":
        mainMenu()
    if answer == "Q" or answer == "q":
        sys.exit()
    


def choiceOne():
    
    """Gets called if the user inputs 1 at the Main Menu"""
    print()
    print("          TRANSLATE          ")
    print("1 - Translate a word in Spanish: ")
    print("2 - Translate a word in French: ")
    print("3 - Exit")
    nextchoice=int(input("Choose a number: "))
    if nextchoice == 1:
             english=input("What word would you like to translate to Spanish: ")
             print(x[english])
             print()
             returnMenu()
        
    if nextchoice == 2:
             english=input("What word would you like to translate to French: ")
             print(y[english])
             returnMenu()
        
    if nextchoice ==3:
             print("Sending you back to main menu in 3 seconds")
             time.sleep(3)
             mainMenu()
             choice=int(input("Choose a number: "))

def choiceTwo():
    
    """Gets called if user selects option 2 at Main Menu"""
    print()
    print("Which Dictionary would you like printed?")
    decide=input("Spanish or French: ")
    if decide == "spanish" or decide == "Spanish":
            
            print("Spanish Dictionary = ",x)
    if decide == "french" or decide == "French":
            print("French Dictionary = ",y)
    
    returnMenu()
    
def choiceThree():
    
    """Gets called when user selects option 3 at Main Menu"""
    print()
    print("          ADD A WORD          ")
    print("Which dictionary would you like to add a word to")
    print("1 - Spanish")
    print("2 - French")
    newchoice=int(input("Choose a number: "))
    while newchoice != 1 and newchoice != 2:
            newchoice=int(input("Please make a selection between 1 and 2 only! "))
    if newchoice == 1:
            word=input("What word would you like to add? ")
            translate=input("What is the translation of the word you entered? ")
            print(word,":",translate)
            x[word]=translate
            print(x)
            
""" This is where i want to take x[word]=translate and send it to dictionary.py so when I open that file in python it is updated to include those words"""
            
            
            altChoice=str(input("Would you like to add another word to a dictionary?: y or n"))
            if altChoice != "y" and altChoice != "Y" and altChoice != "n" and altChoice != "N":
                print("Please only choose y or n: ")
                altChoice=str(input("Would you like to add another word to a dictionary?: y or n"))
            if altChoice == "n" or altChoice == "N":
                returnMenu()
            if altChoice == "y" or altChoice == "Y":
                choiceThree()
            
    else:
            word=input("What word would you like to add? ")
            translate=input("What is the translation of the word you entered? ")
            print(word,":",translate)
            y[word]=translate
            print(y)
            altChoice=str(input("Would you like to add another word to a dictionary?: y or n "))
            while altChoice != "y" or altChoice != "Y" or altChoice != "n" or altChoice != "N":
                print("Please only choose y or n")
                altChoice=str(input("Would you like to add another word to a dictionary?: y or n "))
                if altChoice == "n" or altChoice == "N":
                    returnMenu()
                if altChoice == "y" or altChoice == "Y":
                    choiceThree()


            
def choiceFour():
    """Gets called when user selects option 4 at Main Menu"""
    print("Thank You for using this Translator")
    print("GoodBye")
    sys.exit()


mainMenu()
Reply
#2
What is your question? If you are getting an error, please provide the entire error code between the error tags.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
I am not fixing an error I am trying to write to a .py file from this file.
So if you run the code and choose 3 then choose 1 it will ask what word do you want to add.
user inputs cat
Then the program asks what the translation for the word you want to enter.
user inputs gato
The program then prints out the new dictionary that now includes those words.
Here is where I want to take that updated dictionary that includes those new words and update the .py file I imported the
spanish dictionary from.
Reply
#4
Under option 4, write the Spanish and French dictionaries to the file named dictionary in whatever format they are stored, so the changes will be read the next time you import dictionary. You can also store these in a plain text file, one word and word translated to, as one record separated by a colon or whatever delimiter you want, and read the 2 files, storing them in a dictionary. https://www.idiotinside.com/2015/04/14/e...in-python/
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  reading text file and writing to an output file precedded by line numbers kannan 7 10,247 Dec-11-2018, 02:19 PM
Last Post: ichabod801
  Writing incorrect passwords to a file till its correct garth 2 4,901 Feb-10-2017, 11:41 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020