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


Messages In This Thread
Writing to a .py file - by J0k3r - Apr-18-2018, 12:07 AM
RE: Writing to a .py file - by sparkz_alot - Apr-18-2018, 12:44 PM
RE: Writing to a .py file - by J0k3r - Apr-18-2018, 03:31 PM
RE: Writing to a .py file - by woooee - Apr-18-2018, 07:01 PM

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,605 Dec-11-2018, 02:19 PM
Last Post: ichabod801
  Writing incorrect passwords to a file till its correct garth 2 5,020 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