# tpm.py
# Take two inputs, age and years of citizenship, and returns eligibility for
# senator and representative.

from graphics import *

def main():
    win = GraphWin('Eligibility' , 400,400)
    win.setCoords(0.0,0.0, 4.0, 4.0)

    Text(Point(1, 3.0), 'Age').draw(win)
    Text(Point(1, 2.0), 'Years Citizen').draw(win)
    agebox = Entry(Point(2, 3.0), 10)
    agebox.draw(win)
    citizenshipbox = Entry(Point(2, 2.0), 10)
    citizenshipbox.draw(win)
    output = Text(Point(2.0, 1.0), 'Eligibility')
    output.draw(win)

    win.getMouse()
    age = eval(agebox.getText())
    citizenship = eval(citizenshipbox.getText())


    if 30 > age >= 25:
        if citizenship >= 7:
            output.setText('Eligible for US representative')
        else:
            output.setText('Ineligible for US representative')
    elif age >= 30:
        if citizenship >= 9:
            output.setText('Eligible for US senator and US representative')
        elif 9 > citizenship >= 7:
            output.setText('Ineligible for US senator, eligible for US representative')
        else:
            output.setText('Ineligible for US senator or representative')

        win.getMouse()
        win.close()

main()

:o

Edit: Added code tags, see http://www.daniweb.com/techtalkforums/announcement114-3.html

Dani AI

Generated

Good start, . The eligibility rules you want to encode are: House — age at least 25 and U.S. citizen for at least 7 years; Senate — age at least 30 and U.S. citizen for at least 9 years. The logic needs to test those boundaries clearly and handle bad input.

Main issues to fix

  • Avoid eval(...) on Entry.getText(): it is unsafe and brittle. Use int() and catch ValueError.
  • Indentation bug: your win.getMouse() / win.close() are indented inside the elif block, so they only run for one branch — move them to the end of main() so the window always closes.
  • Use clearer comparisons: 25 <= age < 30 and 7 <= citizenship < 9 (or test >= thresholds in descending order).
  • Validate input (non-numeric, negative, floats) and show an error message instead of crashing.

A clean, reusable approach is to separate the eligibility logic from the GUI. For example:

def eligibility(age, years_citizen):
    if age >= 30:
        if years_citizen >= 9:
            return 'Eligible for US senator and US representative'
        if years_citizen >= 7:
            return 'Ineligible for US senator, eligible for US representative'
        return 'Ineligible for US senator or representative'
    if age >= 25 and years_citizen >= 7:
        return 'Eligible for US representative'
    return 'Ineligible for US senator or representative'

Call it from the GUI with safe conversion:

try:
    age = int(agebox.getText().strip())
    cit = int(citizenshipbox.getText().strip())
except ValueError:
    output.setText('Enter whole numbers only')
else:
    output.setText(eligibility(age, cit))

Thanks to for the console test — that helped pinpoint logical gaps. Test corner cases (age exactly 25 or 30, citizenship exactly 7 or 9) and keep input validation and window-close logic outside conditional branches.

Recommended Answers

All 2 Replies

I don't have the graphics module, but I tested your logic and found it faulty. Here is my test code:

age = int(raw_input("years of age: "))
citizenship = int(raw_input("years citizen: "))

# age 25 to less than 30
if 25 <= age < 30:
    if citizenship >= 7:
        print 'Eligible for US representative'
    else:
        print 'Ineligible for US representative'
# age 30 and more
elif age >= 30:
    if citizenship >= 9:
        print 'Eligible for US senator and US representative'
    elif 7 <= citizenship < 9:
        print 'Ineligible for US senator, eligible for US representative'
# does not meet any of the conditions
else:
    print 'Ineligible for US senator or representative'

Thanks for your help..

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.