Hi am kinda new in python programing. I have this game that involves a user inputs a value , say color it iterates over imported modules( in this case color) and prints data about it. The problem is that i cant get to code the best way to iterate over teh modules. Here is the code:

import games # games package has all modules to be used. The modules include color, food etc

GAMES = ('color', 'food', 'car', 'toy')

while True:
    question = input('Please enter your question: ').lower()
    if not question:
        break
    for g in GAMES:             
        if g in question:
            getattr(games, g)()
            break
    else:
        print('I do not have a game for that! :(')

print('Byebye!')

Here are sample modules:

def color():

    while True:
        question = input('Please enter your question: ').lower().split()
        name = question[:]
        name2= ' '.join(name)
        if item in question:
            import sqlite3
            id = 0
            location = ""
            conn = sqlite3.connect("team.db")
            c = conn.cursor()
            c.execute('select * from scores')
            records = c.fetchall()
            for record in records:
            id = record[0]
            location = record[1]
            if id == name3:
            print (name2.capitalize(),':' '\n',location)# sqlite db has more data on the colors including the JPEG colors etc


        else:
            print('I don\'t know such a color.')

def food():
    question = input('Please enter your question: ').lower().split()
        name = question[:]
        name2= ' '.join(name)
        if item in question:
            import sqlite3
            id = 0
            location = ""
            conn = sqlite3.connect("team.db")
            c = conn.cursor()
            c.execute('select * from scores')
            records = c.fetchall()
            for record in records:
            id = record[0]
            location = record[2]
            if id == name3:
            print (name2.capitalize(),':' '\n',location)# sqlite db has more data on teh foods including the JPEG colors etc

I suggest this if module games.food contains function food(), etc

from importlib import import_module
import games # games package has all modules to be used. The modules include color, food etc

GAMES = ('color', 'food', 'car', 'toy')

while True:
    question = input('Please enter your question: ').lower()
    if not question:
        break
    for g in GAMES:
        if g in question:
            module = import_module('games.' + g)
            getattr(module, g)()
            break
    else:
        print('I do not have a game for that! :(')

print('Byebye!')

It seem that you have the same problem as Maurice_3, and maybe the same teacher :)

Thanks for the input. It was worth it!

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.