Here are some problems:
- This is an English forum. You are more likely to get help if you write English code. I can understand/guess most of your variable names, but some of them might as well be names of your pets as far as my understanding of them. You're welcome to do as you wish, but it would help us to help you.
- I'm a big proponent of OOP that makes sense. Unfortunately, Java programmers (and people taught Python by Java programmers) learn horrible design habits, in particular making classes out of things that aren't objects. A "repeatcheck" object doesn't make as much sense to me as, say, a Fraction, a Queue, a Stack or list or whatever else.
- It's fine to define __magic_methods__ (methods that begin and end with double underscores) but we don't usually call them directly. Instead of object.__str__(), call str(object).
- You asked about a file in your original question, when that wasn't relevant to what you need help with. All that was relevant to us for helping you was working with a list that had words in it. When you ask a question like this, you should make it as direct to desired input and output as possible. For example,
My input is
- Code: Select all
words = ('one', 'two', 'three', 'four', 'five', 'six', 'seven')
and my desired result from this input is
- Code: Select all
[('one', 'two'), ('three', 'four'), ('five', 'six')]
We don't need the extra details of the class, and not just because I think using a class is overkill for this program.
- I'm relatively sure I gave you a solution to your problem (or close to it); you made no mention of it in your response. This can be frustrating for us, if we try to help, and we don't get feedback, we can become discouraged. You could have said, "your solution does work, but I want help modifying what I have here" or "your solution doesn't do [x]" or whatever else, but you should respond to our attempts to help you. (As for this long message with excessive advice; you don't need to go over every point, or explain that your professor is forcing you to use classes, feel free to only talk about solving the problem. Or say whatever you'd like.)
Also, I believe the set
datatype is excellent for a problem like this.
Thanks for your response!
I know its a hardship to understand my Swedish variable names. I have to do so because those who are going to "grade" it are Swedish.
I also believe that a object-oriented code is not necessary here, but the "project" i was given requires that it must contain 1 class.
The reason i didn't respond to your solution was simply because, i did not understand it.
I've never come across either yield, iter or zip, and i dont want to put things in my code that i dont quite frankly understand, since later i have to present to the examinator.
And if im not totally off iter is only used in python 2.x?
Ok, back to topic, i will explain a bit more exclusively.
Someone, I, Stalin, Bush, whomever have written a text file in wordpad.
This text file contains of i.e 500 words, written horizontally, not vertically.
I only want to check 40 of those words at a time, if some words are repeated or not.
I also want the program to be able to read text files with less than 40 words aswell
Edit: This is not actually my assignment, I'm doing it for a classmate who is currently in hospital recovering from a stroke, and he asked me to because he didnt want to get too backed up when he got back. If anybody cares, that is.
Edit nr 2: Im sorry if im not making a lot of sense, its 3:30 AM here in CET.
Edit nr 3: This is the initial problem im having.
- Code: Select all
words_in_file = 
file= input("Which file do you wish to check?")
limit = 40
number_words = 0
while number_words <= limit : # Here is where i wanna make sure only 40 words is appended to my list words_in_file
number_words += 1
f1 = f.readline().split()
for i in f1:
But this fails, all the words are indeed put in my list. I know there is something wrong-o with the loops, but i can't figure what!
P.S Just ignore the misplaced words_in_file, I have no clue how it got there.
Edit 4: I only want to read 40 words at a time, check for repeated words, then go on with the next 40 words, all until the textfile is completely checked.