I would like to make a selection menue for 2 programs that i have. A menu similar to the one below.

import sys
print """Menu
1) Random
2) Search"""

answer = raw_input("Make a selection> ")
if "1" in answer: print "This will contain program 1"
elif "2" in answer: print "This will contain program 2"

Program 1

import re
data = ["1. Principle of Segmentation: \n a) Divide an object into independent parts \n     - Replace a large truck by a truck and trailer. \n     - Use a work breakdown structure for a large project \n     - Multiple Engine on Plane \n b) Make an object easy to disassemble \n     - Bicycle disassembling (saddle, wheels) \n     - Furniture (sofas, table) \n     - Crib \n c) Increase the degree of fragmentation or segmentation \n    - Replace solid shades with Venetian blinds \n    - Computer covers with holes for ventilation \n    - Multi blade cartridge Razor \n", 
        "2. Principle of Local Quality: \n a) car If a structure of an object (or its environment, or external force) \n    is uniform, make it non-uniform \n    - Moulded hand grips on tools \n    - Drink cans shaped to facilitate stable stacking \n b) Reconstruct an object so that different parts perform different functions \n    -Hair dryer (heating and cooling) \n    -Hammer with nail puller \n    -Pencil with eraser \n c) Establish the most suitable operation conditions for every component of \n    an object \n    - Lunch box with special compartments for hot and cold  \n    - Freezer compartment in refrigerator \n ",
        "3. Principle of Asymmetry: \n a) If an object is symmetrical, change its shape to irregular \n    -Asymmetric paddles mix \n    - cement truck \n    - blender \n    - cake mixer \n b) If an object is already of irregular shape, \n    increase the degree of its asymmetry \n    - Asymmetric scissors are easier to use then symmetric ones \n    - Introduction of several different measurement scales on a ruler"] 

for subtext in data:
    if re.search(r"\engine\b", subtext, re.I):
        print "FOUND", subtext

Program 2

import random
import time
raw_input("\n Random Search, press Enter \n")
def points():
      results = ["\n 1. Principle of Segmentation: \n a)Divide an object into independent parts \n - Replace a large truck by a truck and trailer \n - Gator-grip socket spanner  \n - Multi-engined aircraft \n b)Make an object easy to disassemble \n - Bicycle disassembling (saddle, wheels) \n - Furniture (sofas, table) \n - Crib \n c)Increase the degree of fragmentation or segmentation \n - Replace solid shades with Venetian blinds \n - Computer covers with holes for ventilation \n - Multi blade cartridge Razor",
                 "\n 3. Principle of Local Quality: \n a)If a structure of an object (or its environment, or external force) is uneven make it even \n - Moulded hand grips on tools \n - Drink cans shaped to facilitate stable stacking \n b)Reconstruct an object so that different parts perform different functions \n - Hair dryer (heating and cooling) \n - Hammer with nail puller \n - Pencil with eraser \n c)Establish the most suitable operation \n  conditions for every component of an object \n - Lunch box with special compartments for hot and cold \n Freezer compartment in refrigerator",
                 "\n 4. Principle of Asymmetry: \n a)If an object is symmetrical, change its shape to irregular \n - Asymmetric paddles mix \n - cement truck \n - blender \n - cake mixer \n b)If an object is already of irregular shape \n  increase the degree of its asymmetry \n - Asymmetric scissors are easier to use then symmetric ones \n - Introduction of several different measurement scales on a ruler",
                 "\n 5. Principle of Combination: \n a)Combine like objects or objects intended for similar or related operations \n - Combine computers on a network \n - Fragile and weak components, such as glass plates can be made stronger \n   without increasing weight by combining them to packages",
                 "\n 6. Principle of University: \n a)Design an object to perform multiple functions \n  so that some other objects are no longer needed \n - Use a single adjustable wrench for all nuts \n - Team leader can act a recorder and time keeper \n - Toothbrush with dental floss"] 
      rand1 = random.choice(results)
      rand2 = random.choice(results)
      rand3 = random.choice(results)
      time.sleep(1)
      print rand1
      time.sleep(1.5)
      print rand2
      time.sleep(2)
      print rand3
      time.sleep(1)

   
loop = 1
while loop == 1:
   points()
   z = raw_input("\nWere the hints Helpfull?")
   if z == "no": 
      continue 
   else:
      loop = 0

Program 1 Searches for the word in the Principle and Program 2 generates by random. I tried combining them into the menue so the user can chose which program they would like to run but i keep getting errors.
Any Help will be appreciated.

Thank you.

Recommended Answers

All 8 Replies

I think the solution is to write more functions:

import random
import re
import time
import sys

data = [
  "1. Principle of Segmentation: \n a) Divide an object into independent parts \n     - Replace a large truck by a truck and trailer. \n     - Use a work breakdown structure for a large project \n     - Multiple Engine on Plane \n b) Make an object easy to disassemble \n     - Bicycle disassembling (saddle, wheels) \n     - Furniture (sofas, table) \n     - Crib \n c) Increase the degree of fragmentation or segmentation \n    - Replace solid shades with Venetian blinds \n    - Computer covers with holes for ventilation \n    - Multi blade cartridge Razor \n",
  etc...
]

def word_search():
   word = raw_input("Enter the word to search> ")
   for subtext in data:
       if re.search(r"\b%s\b" % word, subtext, re.I):
           print "FOUND", subtext

def point():
    result = data
    rand1 = random.choice(results)
    etc...

def random_search():
   loop = 1
   while loop == 1:
      points()
      z = raw_input("\nWere the hints Helpfull?")
      etc...

def main():
    print """Menu
1) Random
2) Search"""

    answer = raw_input("Make a selection> ")
    if "1" in answer:
        random_search()
    elif "2" in answer:
        word_search()

if __name__ == "__main__":
    main()

Thank you!
Search works, but when i select one for random, i get the following error:

Traceback (most recent call last):
File "C:\Users\user\Desktop\final menu not done.py", line 48, in <module>
main()
File "C:\Users\user\Desktop\final menu not done.py", line 43, in main
random_search()
NameError: global name 'random_search' is not defined

import random
import re
import time
import sys

data = [ "1. Principle of Segmentation: \n a) Divide an object into independent parts \n     - Replace a large truck by a truck and trailer. \n     - Use a work breakdown structure for a large project \n     - Multiple Engine on Plane \n b) Make an object easy to disassemble \n     - Bicycle disassembling (saddle, wheels) \n     - Furniture (sofas, table) \n     - Crib \n c) Increase the degree of fragmentation or segmentation \n    - Replace solid shades with Venetian blinds \n    - Computer covers with holes for ventilation \n    - Multi blade cartridge Razor \n",]

def word_search():
    word = raw_input("Enter the word to search> ")
    for subtext in data:
        if re.search(r"\b%s\b" % word, subtext, re.I):
            print "FOUND", subtext

def point():
    result = data
    rand1 = random.choice(results)
    rand2 = random.choice(results)
    rand3 = random.choice(results)
    time.sleep(1)
    print rand1
    time.sleep(1.5)
    print rand2
    time.sleep(2)
    print rand3
    time.sleep(1)

    def random_search():
        loop = 1   
        while loop == 1:
            points()
            z = raw_input("\nWere the hints Helpfull?")
            if z == "no":
                   continue
            else:      loop = 0
            
def main():
    print """Menu
1) Random
2) Search"""

    answer = raw_input("Make a selection> ")
    if "1" in answer:
        random_search()
    elif "2" in answer:
            word_search()

if __name__ == "__main__":
    main()

It's because you wrote the random_search() function inside the function point(). Indentation is very important in python.

I have been trying for the last 45 minutes.
But i realy do no see it.

I have been trying for the last 45 minutes.
But i realy do no see it.

write "def random_search()" at the beginning of the line.

This?
I get no error, but i also get no result either. Nothing.

import random
import re
import time
import sys

data = [ "1. Principle of Segmentation: \n a) Divide an object into independent parts \n     - Replace a large truck by a truck and trailer. \n     - Use a work breakdown structure for a large project \n     - Multiple Engine on Plane \n b) Make an object easy to disassemble \n     - Bicycle disassembling (saddle, wheels) \n     - Furniture (sofas, table) \n     - Crib \n c) Increase the degree of fragmentation or segmentation \n    - Replace solid shades with Venetian blinds \n    - Computer covers with holes for ventilation \n    - Multi blade cartridge Razor \n",
         "2. Principle of Local Quality: \n a) car If a structure of an object (or its environment, or external force) \n    is uniform, make it non-uniform \n    - Moulded hand grips on tools \n    - Drink cans shaped to facilitate stable stacking \n b) Reconstruct an object so that different parts perform different functions \n    -Hair dryer (heating and cooling) \n    -Hammer with nail puller \n    -Pencil with eraser \n c) Establish the most suitable operation conditions for every component of \n    an object \n    - Lunch box with special compartments for hot and cold  \n    - Freezer compartment in refrigerator \n ",
         "3. Principle of Asymmetry: \n a) If an object is symmetrical, change its shape to irregular \n    -Asymmetric paddles mix \n    - cement truck \n    - blender \n    - cake mixer \n b) If an object is already of irregular shape, \n    increase the degree of its asymmetry \n    - Asymmetric scissors are easier to use then symmetric ones \n    - Introduction of several different measurement scales on a ruler"] 

def word_search():
    word = raw_input("Enter the word to search> ")
    for subtext in data:
        if re.search(r"\b%s\b" % word, subtext, re.I):
            print "FOUND", subtext

def point():
    result = data
    rand1 = random.choice(results)
    rand2 = random.choice(results)
    rand3 = random.choice(results)
    time.sleep(1)
    print rand1
    time.sleep(1.5)
    print rand2
    time.sleep(2)
    print rand3
    time.sleep(1)


    loop = 1   
    while loop == 1:
            points()
            z = raw_input("\nWere the hints Helpfull?")
            if z == "no":
                   continue
            else:      loop = 0
            
def main():
    print """Menu
1) Random
2) Search"""

    answer = raw_input("Make a selection> ")
    if "1" in answer:
        def random_search():
         random_search()
    elif "2" in answer:
            word_search()

if __name__ == "__main__":
    main()

Add def random_search() immediately before line 30, at the beginning of the line. You must understand how the program works, not only copy advices. This program has a simple structure.

Believe me i know that.
I had it at the location before the previous post.
I still get an error:

NameError: global name 'points' is not defined

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.