#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2

    Question Random Sentence Generator (Need help with 1 problem)


    Code:
    #159171 - Assignment 5, KHALID ELTAYEB, 12233035
    
    import sys, random, os.path
    def loadFileIntoList(filename, whichDir):
       listOfWords = []
       try:
          fullName = os.path.join(whichDir, filename) # join directory & filename 
          fileHandle = open(fullName, "r")            # open file for reading ("r")
          while True:                            # Repeatedly
              line = fileHandle.readline()          #  get the next line from the file
              if line == "":     break             #  if it's null string: stop - no more lines
              line = line.strip()              #  remove leading and trailing spaces and newline
              if line != "":                 #  If there's anything left 
                  listOfWords.append(line)    #     add it to the list of words   
          return listOfWords
       except IOError:                    # if an IO error occur, this part is executed                
          printNow("EMERGENCY STOP: couldn't find " + fullName)
          sys.exit(1)
    
    workDir = pickAFolder()
    adjectiveList=loadFileIntoList('Adjectives.txt',workDir)   
    adverbList=loadFileIntoList('Adverbs.txt',workDir)            
    conjunctionList=loadFileIntoList('Conjunctions.txt',workDir)        
    instransitiveVerbList=loadFileIntoList('IntransitiveVerbs.txt',workDir)    
    leadinList=loadFileIntoList('Leadin.txt',workDir)              
    nounMarkerList=loadFileIntoList('NounMarkers.txt',workDir)         
    nounList=loadFileIntoList('Nouns.txt',workDir) 
    transitiveVerbList=loadFileIntoList('TransitiveVerbs.txt',workDir)
    
    def Test():
      for i in range(1):
        adjective=adjectiveList[i]
        adverb=adverbList[i]
        conjunction=conjunctionList[i]
        instransitiveVerb=instransitiveVerbList[i]
        leadin=leadinList[i]
        nounMarker=nounMarkerList[i]
        noun=nounList[i]
        transitiveVerb=transitiveVerbList[i]
        printNow(adjective+'\n'+adverb+'\n'+conjunction+'\n'+instransitiveVerb+'\n'+leadin+'\n'+nounMarker+'\n'+noun+'\n'+transitiveVerb)
      
    def EasyS():
      for i in range (1):
        noun=random.choice(nounList)
        instransitiveVerb=random.choice(instransitiveVerbList)
        printNow(noun.title()+' '+instransitiveVerb+'.')
    
    
    
    def NewS():
      for i in range(1):
        adjective=random.choice(adjectiveList)
        adverb=random.choice(adverbList)
        conjunction=random.choice(conjunctionList)
        instransitiveVerb=random.choice(instransitiveVerbList)
        leadin=random.choice(leadinList)
        nounMarker=random.choice(nounMarkerList)
        noun=random.choice(nounList)
        transitiveVerb=random.choice(transitiveVerbList)
        printNow(leadin.title()+' '+nounMarker+' '+adjective+' '+noun+' '+adverb+' '+instransitiveVerb+' '+transitiveVerb+nounMarker+' '+adjective+' '+noun+'.')
        
      
    printNow("Enter L to Load all the files of words from disk")
    printNow("Enter T to Test - display the first word from each list to make sure they've been loaded.")
    printNow("Enter E to generate an easy sentence - randomly selected noun followed by a randomly selected verb and then a full stop.")
    printNow("Enter S to generate a new sentence - a sentence conforming to the specified grammar.")
    printNow("Press Q to quit")
    
    def main():
      task = raw_input("Please enter a command(L/T/E/S/Q): ")
      if task == "L":
        main()
      
      elif task == "T": 
        Test()
        main()
      
      elif task == "E":
        EasyS()
        main()
      
      elif task == "S":
        NewS()
        main()
      
      elif task == "Q":
        printNow("Have a nice day.")
      
      else:
        printNow("You have entered an invalid command, please try again.")
        main()
        
    main()

    I have written the program above which lets the user pick a file and then the user has 5 choices on what to do with the file..
    I have two problems i need help with:



    Problem:
    I need to edit the code so that if the user doesnt choose "L" (Loads all the files of words from disk) as his/her first option it should display an error message because the user has to load the files of words before he can generate any sentence or words.


    this program took me so long to do but im just stuck at that one problem
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Do you have to have "L" as an option then? Sounds like the program should simply load the files automatically if all options depend on it.

    If this is not an option, I would add a boolean, e.g. "filesLoaded", which is initialized to False and then set to True after the user has chosen the "L" option. After that you can simply check if "filesLoaded" is set to true if the user choses any other option.
    http://www.catb.org/esr/faqs/smart-questions.html#code
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2
    Originally Posted by partoj
    Do you have to have "L" as an option then? Sounds like the program should simply load the files automatically if all options depend on it.

    If this is not an option, I would add a boolean, e.g. "filesLoaded", which is initialized to False and then set to True after the user has chosen the "L" option. After that you can simply check if "filesLoaded" is set to true if the user choses any other option.
    thnx for the quick reply, but how can i do what you just told me? can u please put it in code?
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    Have you got a pickAFolder function? Probably. Comes from jython.

    This recursion looks clean and smart
    Code:
    def main():
      task = raw_input("Please enter a command(L/T/E/S/Q): ")
      if task == "L":
        main()
    Bad practice. With long use the program will generate a stack fault for no good reason. Use a loop instead.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2
    Originally Posted by b49P23TIvg
    Have you got a pickAFolder function? Probably. Comes from jython.

    This recursion looks clean and smart
    Code:
    def main():
      task = raw_input("Please enter a command(L/T/E/S/Q): ")
      if task == "L":
        main()
    Bad practice. With long use the program will generate a stack fault for no good reason. Use a loop instead.
    Yes, i got pickAFolder function in my program...this problem is just giving me a headache because i cant seem to understand how to solve it, how can do the error command if the user doesnt choose load files as his/her first option?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2
    Originally Posted by kidoman
    Yes, i got pickAFolder function in my program...this problem is just giving me a headache because i cant seem to understand how to solve it, how can do the error command if the user doesnt choose load files as his/her first option?
    Code:
    def main():
      what to type here?
      task = raw_input("Please enter a command(L/T/E/S/Q): ")
      while true:
      if task == "L":
        main()
    would i add 'while true:' in that place? and what do i type after def main?
    need urgent help please
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    Code:
    def main():
      while True:
        task = raw_input("Please enter a command(L/T/E/S/Q): ")
        if task == "Q":
          break
        #handle other cases
      print('closing message')
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2
    Originally Posted by b49P23TIvg
    Code:
    def main():
      while True:
        task = raw_input("Please enter a command(L/T/E/S/Q): ")
        if task == "Q":
          break
        #handle other cases
      print('closing message')
    thnx but im still not sure how this works so it displays the error message if he doesnt choose L first -_-
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    partoj answered this problem in post 2. If there's no choice, don't ask.
    [code]Code tags[/code] are essential for python code and Makefiles!
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2
    Originally Posted by b49P23TIvg
    partoj answered this problem in post 2. If there's no choice, don't ask.
    yh he did, but i dont know how to put it in code form..how do u set it to false then true if user chooses L?

IMN logo majestic logo threadwatch logo seochat tools logo