Did I do this right and can you please help me. This program uses a GUI to input a user's first and last name,
and construct and display a UserID. Although the program 'runs',
there are several problems with it. Fix each of these problems
in turn:

1) The text and corresponding entry box for 'Last Name' does
not align with those for 'First Name'. Correct this.
2) Provide the code to draw a Rectangle around the 'button'
text object, so that it looks like a button.
3) The program as written always produces the same UserID, 'frodo'.
Change this so the UserID will be the first six letters of the
last name and the first character of the first name, all in
lowercase. For example, if the user entered 'Bilbo' and 'Baggins'
for first and last names, respectively, his UserId would be
'bagginb'. [Hint: you may want to use the lower() function is
the string library for this.]
4) Add the code necessary to change the text of the button text
object to 'Quit' after the UserID has been displayed :o :o

from graphics import * 

def main(): 

win = GraphWin("UserID Assignment", 400, 300) 
win.setCoords(0,0, 4,3) 

# draw the interface 
Text(Point(0.5, 2.5), "First Name").draw(win) 
Text(Point(0.8, 1.7), " Last Name").draw(win) 
fName = Entry(Point(2.0, 2.5), 20) 
fName.setText("") 
fName.draw(win) 
lName = Entry(Point(2.5, 2.0), 20) 
fName.setText("") 
lName.draw(win) 
button = Text(Point(2.0, 1.0), "Get UserID") 
button.draw(win) 

output = Text(Point(2.0, 0.3), \ 
"Enter your name and click 'Get UserID'") 
output.draw(win) 

# wait for mouse click 
win.getMouse() 

# obtain input and construct userid 
first = fName.getText() 
last = lName.getText() 
userid = "frodo" 

# display result 
output.setText("Your UserID is " + userid + ".") 

# all done 
win.getMouse() 
win.close() 

main()

Dani AI

Generated

A concise, practical fix that covers all four tasks: align the labels and entry fields by using the same baseline x/y positions; draw a Rectangle behind the button text and test clicks against it; construct the userid as the first six letters of the last name plus the first initial of the first name (all lowercase), handling short names safely; and change the button text to "Quit" after the UserID is shown so a second click closes the window. Good catch, — using lower() and slicing is the right idea; the code below implements that and adds basic input checks. No need to apologize, — these are exactly the sorts of small fixes that make GUI code work.

from graphics import *

def point_in_rect(rect, p):
    p1, p2 = rect.getP1(), rect.getP2()
    xmin, xmax = min(p1.getX(), p2.getX()), max(p1.getX(), p2.getX())
    ymin, ymax = min(p1.getY(), p2.getY()), max(p1.getY(), p2.getY())
    return xmin <= p.getX() <= xmax and ymin <= p.getY() <= ymax

def main():
    win = GraphWin("UserID GUI", 400, 260)
    win.setCoords(0,0,4,2.6)

    label_x, entry_x = 0.6, 2.2
    row1_y, row2_y = 1.8, 1.3

    Text(Point(label_x, row1_y), "First Name").draw(win)
    first = Entry(Point(entry_x, row1_y), 20); first.draw(win)

    Text(Point(label_x, row2_y), "Last Name").draw(win)
    last = Entry(Point(entry_x, row2_y), 20); last.draw(win)

    bx, by, bw, bh = entry_x, 0.7, 0.9, 0.35
    btn = Rectangle(Point(bx-bw/2, by-bh/2), Point(bx+bw/2, by+bh/2))
    btn.setFill("lightgray"); btn.draw(win)
    btnText = Text(Point(bx, by), "Get UserID"); btnText.draw(win)

    output = Text(Point(entry_x, 0.35), "Click the button to get a UserID."); output.draw(win)

    while True:
        click = win.getMouse()
        if point_in_rect(btn, click):
            f = first.getText().strip()
            l = last.getText().strip()
            if not l:
                output.setText("Please enter a last name.")
                continue
            last_part = l.lower()[:6]
            first_part = f[0].lower() if f else ""
            userid = last_part + first_part
            output.setText("Your UserID is " + userid + ".")
            btnText.setText("Quit")
            if point_in_rect(btn, win.getMouse()):
                break
        else:
            output.setText("Click the button to get a UserID.")

    win.close()

if __name__ == "__main__":
    main()

Notes and common gotchas: avoid leading spaces in label strings (that causes visual misalignment), don't call the wrong widget's setText (the original setText calls were mixed up), and indent the body of main() properly as pointed out. If the environment doesn't include Zelle's graphics, use tkinter (layout and event handling differ).

Recommended Answers

All 2 Replies

The module "graphics" is foreign to me. Is this something your teacher has writtten?

Also everything belonging to function main() should be indented properly!

Replace
userid = "frodo"
with
userid = last.lower()[:6] + first.lower()[:1]

im only in the 8th grade srry

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.