所以我做了一些功课,而且我有点难过,这项任务是创建一个程序,根据你的名字和你开始上学的那一年制作一个电子邮件地址,但对于一些人来说我无法收到生成电子邮件的原因我继续将" String索引超出范围"错误。 以下是信息:
Traceback (most recent call last):
File "C:/Users/simps/Desktop/email.py", line 58, in <module>
emailOutput = Text(Point(200,350), fst_name[0] + lst_name + YEAR + "@student.kathycollege.edu")
IndexError: string index out of range
Here is my code:
# a graphical program that creates a student email adress
from graphics import *
import math
#setting up the window
win = GraphWin("email",400,400)
win.setBackground("yellow")
#setting up the input
instructions = Text(Point(160,50), "For your email, enter your first and last name,\n and the year you are starting school.")
instructions.draw(win)
fstName = Text(Point(100,100),"What is your first name?: ")
fstName.draw(win)
fstNameInput = Entry(Point(260,100), 10)
fstNameInput.draw(win)
lstName = Text(Point(100,150), "what is your last name?: ")
lstName.draw(win)
lstNameInput = Entry(Point(260,150), 15)
lstNameInput.draw(win)
year = Text(Point(130,200), "What year are you starting school?: ")
year.draw(win)
yearInput = Entry(Point(280,200), 4)
yearInput.draw(win)
#Line break
linBreak = Text(Point(200,289), "=======================================")
linBreak.draw(win)
#Output
email = Text(Point(50,315), "Your email is: ")
email.draw(win)
emailOutput = Entry(Point(200,340), 50)
emailOutput.draw(win)
#setting up The process
fst_name = fstNameInput.getText()
lst_name = lstNameInput.getText()
YEAR = yearInput.getText()
#Button
button = Rectangle(Point(130,250),Point(210,279))
button.draw(win)
buttonCenter = button.getCenter()
buttonText = Text(buttonCenter, "Enter")
buttonText.draw(win)
#button function (got help for this on stack overflow)
def inside(point, rectangle):
ll = rectangle.getP1()
ur = rectangle.getP2()
return ll.getX() < point.getX() < ur.getX() and ll.getY() < point.getY() < ur.getY()
while True:
click = win.getMouse()
if click is None:
emailOutput.setText("")
elif inside(click, button):
emailOutput.setText(fst_name[0] + lst_name + YEAR + "@student.kathycollege.edu")
break;
答案 0 :(得分:0)
错误消息会有所帮助。 但基于我的眼球,错误可能发生在最后一行:
if (x == null || y == null) return x == null ? y == null ? new T[0] : y : x;
当emailOutput.setText(fst_name[0] + lst_name + YEAR + "@student.kathycollege.edu")
为空时,fst_name
会使索引超出范围错误。我不认为你在这里需要fst_name[0]
,或者你可能想要单独处理无名字的情况。
答案 1 :(得分:0)
我想通了,我不得不将getText变量移动到按钮命令,所以这些只会在我点击按钮后发生