Question

Hi I am using python and I keep getting this error with my code. Traceback (most...

Hi I am using python and I keep getting this error with my code. Traceback (most recent call last):
['1']
Traceback (most recent call last):
File "/Users/isabellawelch/Python ish/shuffle.py", line 86, in <module>
emp = Employee(emp_data[0], emp_data[1], emp_data[2], emp_data[3], emp_data[4])
IndexError: list index out of range

What does it mean and how do i fix it??

from tkinter import Tk, Label, Button, Entry, END

employees = []

class Employee:
def __init__(self, empno, name, addr, hwage, hworked):
self.empno = empno
self.name = name
self.addr = addr
self.hwage = hwage
self.hworked = hworked

def calc_salary(self):
self.gpay = float(self.hwage) * float(self.hworked)
self.netpay = self.gpay - self.gpay * 0.20 - self.gpay * 0.075
return self.netpay

class MyFirstGUI:
def __init__(self, master):
self.cnt = 0
self.master = master
master.title("Fluff Shuffle Electronics")

self.lEmpNo = Label(master, text="Emp Number: ")
self.txtEmpNo = Entry(master, validate="key", text="Hello")
self.lName = Label(master, text="Name: ")
self.txtName = Entry(master, validate="key")
self.lAddr = Label(master, text="Address: ")
self.txtAddr = Entry(master, validate="key")
self.lhw = Label(master, text="Hourly Wage: ")
self.txthw = Entry(master, validate="key")
self.lhwkd = Label(master, text="Hours Worked: ")
self.txthwkd = Entry(master, validate="key")
self.lNetpay = Label(master, text="Net Pay: ")
self.txtNetpay = Entry(master, validate="key")

self.btnNext = Button(master, text="Next Employee", command=lambda: self.update("next"))
self.btnClose = Button(master, text="Close", command=master.quit)

self.lEmpNo.grid(row=0, column=0)
self.txtEmpNo.grid(row=0, column=1)
self.lName.grid(row=1, column=0)
self.txtName.grid(row=1, column=1)
self.lAddr.grid(row=2, column=0)
self.txtAddr.grid(row=2, column=1)
self.lhw.grid(row=3, column=0)
self.txthw.grid(row=3, column=1)
self.lhwkd.grid(row=4, column=0)
self.txthwkd.grid(row=4, column=1)
self.lNetpay.grid(row=5, column=0)
self.txtNetpay.grid(row=5, column=1)
self.btnNext.grid(row=6, column=0)
self.btnClose.grid(row=6, column=1)

self.update("next")

def greet(self):
print("Greetings!")

def update(self, method):
if method == "next":
print(employees[self.cnt].name)
emp = employees[self.cnt]
self.txtEmpNo.delete(0, END)
self.txtEmpNo.insert(0, emp.empno)
self.txtName.delete(0, END)
self.txtName.insert(0, emp.name)
self.txtAddr.delete(0, END)
self.txtAddr.insert(0, emp.addr)
self.txthw.delete(0, END)
self.txthw.insert(0, emp.hwage)
self.txthwkd.delete(0, END)
self.txthwkd.insert(0, emp.hworked)
self.txtNetpay.delete(0, END)
self.txtNetpay.insert(0, emp.calc_salary())
self.cnt += 1
self.cnt = self.cnt % len(employees)

if __name__ == "__main__":
file_ref = open("/Users/isabellawelch/Downloads/data.txt", 'r')
line = file_ref.readline()
while line:
line = line.strip('\n')
emp_data = line.split(',')
print(emp_data)
emp = Employee(emp_data[0], emp_data[1], emp_data[2], emp_data[3], emp_data[4])
employees.append(emp)
line = file_ref.readline()
root = Tk()
my_gui = MyFirstGUI(root)
root.mainloop()

0 0
Add a comment Improve this question Transcribed image text
Answer #1

As you can see only one data ['1'] that printed for emp_data but in here

Employee(emp_data[0], emp_data[1], emp_data[2], emp_data[3], emp_data[4])

required atleast five data to work on that's why you getting index error,

and can you check your file /Users/isabellawelch/Downloads/data.txt

that have sufficient entry it should have min 5 data seprated by comma.

lets say just for example if file have 1,2,3,4,5 have date then emp_data will have ['1','2','3','4','5'] then Index Error will not occur

please try if not then comment will help you further.

Add a comment
Know the answer?
Add Answer to:
Hi I am using python and I keep getting this error with my code. Traceback (most...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help with Intro to Comp Sci 2 (Python) problem: This is what is provided: (Copy/Paste...

    Need help with Intro to Comp Sci 2 (Python) problem: This is what is provided: (Copy/Paste version): from tkinter import Tk, Label, Entry, Button from random import * class Craps(Tk): #set up the main window def __init__(self, parent=None): Tk.__init__(self, parent) self.title('Play Craps') self.new_game() self.make_widgets() #when a new game is started, the firstRoll will start at 0 def new_game(self): self.firstRoll = 0    #create and place the widgets in the window def make_widgets(self): Label(self, text="Die 1").grid(row=0, column=0, columnspan=1) Label(self, text="Die 2").grid(row=0,...

  • Help with Python code. Right now I'm creating a code in Python to create a GUI....

    Help with Python code. Right now I'm creating a code in Python to create a GUI. Here's my code: #All modules are built into Python so there is no need for any installation import tkinter from tkinter import Label from tkinter import Entry def calculatewages(): hours=float(nhours.get()) nsal=float(nwage.get()) wage=nsal*hours labelresult=Label(myGUI,text="Weekly Pay: $ %.2f" % wage).grid(row=7,column=2) return Tk = tkinter.Tk()    myGUI=Tk myGUI.geometry('400x200+100+200') myGUI.title('Pay Calculator') nwage=float() nhours=float() label1=Label(myGUI,text='Enter the number of hours worked for the week').grid(row=1, column=0) label2=Label(myGUI,text='Enter the pay rate').grid(row=2, column=0)...

  • The Gui has all the right buttons, but from there i get lost. I need to...

    The Gui has all the right buttons, but from there i get lost. I need to know whats wrong with my assignment can someone please help. The code I have so far is listed below, could you please show me the errors in my code. PYTHON Create the GUI(Graphical User Interface). Use tkinter to produce a form that looks much like the following. It should have these widgets. Temperature Converter GUI Enter a temperature (Entry box)                 Convert to Fahrenheit...

  • Demonstrate this result for the parallel calculation. CODE: from Tkinter import * # MODIFY THIS TO...

    Demonstrate this result for the parallel calculation. CODE: from Tkinter import * # MODIFY THIS TO HANDLE THREE RESISTORS __author__ = "robincarr" """ GUI version of Resistor Calculator finds the equivalent resistance for two resistors connected either in series or parallel. Features entry widgets and buttons.""" def series_calculation(): r1 = float(resistor1.get()) r2 = float(resistor2.get()) req = r1 + r2 equivalent_resistance.delete(0, END) # Clear the previous result. equivalent_resistance.insert(0, req) print("Resistor 1: %s\nResistor 2: %s" % (r1, r2)) print "The equivalent series...

  • Here is my code for minesweeper in python and it has something wrong. Could you please help me to fix it? import tkinter as tk import random class Minesweeper: def __init__(self): self.main = tk.Tk()...

    Here is my code for minesweeper in python and it has something wrong. Could you please help me to fix it? import tkinter as tk import random class Minesweeper: def __init__(self): self.main = tk.Tk() self.main.title("mine sweeper") self.define_widgets() self.mines = random.sample( [(i,j) for i in range(25) for j in range(50) ],self.CustomizeNumberOfMines()) print(self.mines) self.main.mainloop() self.CustomizeNumberOfMines() def define_widgets(self): """ Define a canvas object, populate it with squares and possible texts """ self.canvas = tk.Canvas(self.main, width = 1002, height=502, bg="#f0f0f0") self.canvas.grid(row=0, column=0) self.boxes =...

  • ########### Homework 20 ################################## ################################################### #### Rewrite this code in 'class mode' : define the class...

    ########### Homework 20 ################################## ################################################### #### Rewrite this code in 'class mode' : define the class and so on.... #### If you can - try make a clicked button 'blue'. #### (Only if you can.) from tkinter import * abc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' def callback(x): label.configure(text = 'Button {} clicked'. format(abc[x])) root = Tk() label = Label() label.grid(row = 1, column = 0, columnspan = 26) buttons = [0]*26 ##create a list to hold 26 buttons for i in range(26): buttons[i]...

  • I am currently facing a problem with my python program which i have pasted below where i have to design and implement python classes and record zoo database and takes user input as query. the error i...

    I am currently facing a problem with my python program which i have pasted below where i have to design and implement python classes and record zoo database and takes user input as query. the error i am recieving says that in line 61 list index is out of range. kindly someone help me with it as soon as possible. Below is the program kindly check and correct it. Thanks! class Animal: def __init__(self, name, types, species, mass): self.name=name self.type=types...

  • Python 3 Fix the code so when i click on submit botton the values reset Code:...

    Python 3 Fix the code so when i click on submit botton the values reset Code: import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook from tkinter import messagebox from datetime import datetime window = tk.Tk() window.title("daily logs") window.grid_columnconfigure(1,weight=1) window.grid_rowconfigure(1,weight=1) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="sold by").grid(row=3, sticky="W", pady=20, padx=20) tk.Label(window, text="Working product").grid(row=4, sticky="W", pady=20, padx=20) #Working product label tk.Label(window, text="Failed...

  • I am currently facing a problem with my python program which i have pasted below where...

    I am currently facing a problem with my python program which i have pasted below where i have to design and implement python classes and record zoo database and takes user input as query. the error i am recieving says that in line 61 list index is out of range. kindly someone help me with it as soon as possible. Below is the program kindly check and correct it. Thanks! class Animal: def __init__(self, name, types, species, mass): self.name=name self.type=types...

  • Python 3 Code does not save the data into excel properly ​​​​​​​ # required library import...

    Python 3 Code does not save the data into excel properly ​​​​​​​ # required library import tkinter as tk from tkcalendar import DateEntry import xlsxwriter # frame window = tk.Tk() window.title("daily logs") #window.resizable(0,0) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="Failed date").grid(row=3, sticky="W", pady=20, padx=20) # entries barcode = tk.Entry(window) product = tk.Entry(window) money = tk.Entry(window) # arraging barcode.grid(row=0, column=1) product.grid(row=1, column=1) money.grid(row=2, column=1) cal =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT