Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How can I fix the key error in python?

+3 votes
asked Nov 22 by Nirali Patel (470 points)
with open('hw3a.txt') as newfile:
    x = [line.strip() for line in newfile]
def majority_item(x):
    counter = 0
    box = {}
    for item in x:
        if box == {}:
            box[item] = 1
        if box == {item}:
            box[item] += 1
        else:
            box = {}
            box[item] = 1
            for item in x:
                box[item] -= 1
                if counter == 0:
                    box = {}
print(majority_item(x))

2 Answers

0 votes
answered Nov 22 by Peter Minarik (63,320 points)

Can you be more specific about what the error is?

Can you share a sample hw3a.txt so one can test your code and see how it behaves?

commented Nov 22 by Nirali Patel (470 points)
how can I share the text file? I am not able to share
commented Nov 22 by Peter Minarik (63,320 points)
You can share the link to your project that has both the source and the input file. SHARE is the orange button 3 to the right from the RUN button. Of course, first you'll need to SAVE your project to share the up-to-date state.
commented Nov 22 by Nirali Patel (470 points)
I tried sharing, but the input file has over million values, so I get error that source file can't be larger than 200kb. How can I share now?
commented Nov 22 by Nirali Patel (470 points)
I tried sharing other way, but not sure if this link works.
https://onlinegdb.com/v74y13Byj
This just has the code because input file is larger so I am not able to save nor share.
commented Nov 23 by Peter Minarik (63,320 points)
The file must have similar records in it over and over. Just have a few records in it (e.g. the first 5 lines) and it should be fine.

But if the file is too large to upload to OnlineGDB, then you cannot run your code with the input here. Is that the problem you posted about in the first place that you cannot provide input to your program?
commented 6 days ago by Nirali Patel (470 points)
No, I am doing it in python editor but I am unable to fix this error, so just posted here. But the values are different in the file, its not repetitive.
commented 6 days ago by Peter Minarik (63,320 points)
I'd suggest grabbing the first few lines from your input file and uploading it to your previously shared project (https://onlinegdb.com/v74y13Byj) so people can test your code. If it's run right now, it fails because the input file cannot be found, hence we cannot even get to the point where your error happens.
commented 6 days ago by Nirali Patel (470 points)
commented 5 days ago by Peter Minarik (63,320 points)
Now I better understand the problem.

If you want to see if an item is present in a list, don't do

if box[item] == {}:

instead, you can check if it exist by

if item in box:

Refactor your code and see how you go.

The Key Error means that you tried to access a specific item in your box but that item does not exist.
commented 5 days ago by Nirali Patel (470 points)
okay, thanks I fixed that code.
For this code, I get indexerror: list index out of range, but I don't understand the s[r], why is it out of range.
https://onlinegdb.com/GrIIDQL94
0 votes
answered 5 days ago by Kamal Lukka (240 points)
code is correct but there no file created in your directory..
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...