2

So I'm thinking this is one of those problems where I can't see the forest for the tree. Here is the assignment:

Using the file object input, write code that read an integer from a file called rawdata into a variable datum (make sure you assign an integer value to datum). Open the file at the beginning of your code, and close it at the end.

okay so first thing: I thought the input function was for assigning data to an object such as a variable, not for reading data from an object. Wouldn't that be read.file_name ?

But I gave it shot:

infile = open('rawdata','r')
datum = int(input.infile())
infile.close()

Now first problem... MyProgrammingLab doesn't want to grade it. By that I mean I type in the code, click 'submit' and I get the "Checking" screen. And that's it. At the time of writing this, my latest attempt to submit as been 'checking' for 11 minutes. It's not giving me an error, it's just not... 'checking' I guess.

Now at the moment I can't use Python to try the program because it's looking for a while and I'm on a school computer that is write locked, so even if I have the code right (I doubt it), the program will fail to run because it can neither find the file rawdata nor create it.

So... what's the deal? Am I reading the instructions wrong or is it telling me to use input in some other way then I'm trying to use it? Or am I supposed to be using a different method?

8
  • Different naming of vaiables Commented Oct 17, 2017 at 17:11
  • The instructions are a bit foggy but I'm interpreting them as "call open and assign the result to the name input". The behavior of the built-in input function doesn't matter in that case, since you're overshadowing it entirely. (Making built-in functions inaccessible this way is usually bad practice, but do what you have to do to get a good grade, I guess) Commented Oct 17, 2017 at 17:11
  • One thing to note, infile represents the open file object and is NOT a method for input()! Commented Oct 17, 2017 at 17:14
  • Alternate interpretation: it means "using the data from a file object as input...", in which case the built-in function input is not involved at all. Commented Oct 17, 2017 at 17:14
  • 2
    Perhaps the real lesson of this assignment is in dealing with ambiguous client requirements ;-) Commented Oct 17, 2017 at 17:16

2 Answers 2

2

You are so close. You're just using the file object slightly incorrectly. Once it's open, you can just .read() it, and get the value.

It would probably look something like this

infile = open('rawdata','r')
datum = int(infile.read())
infile.close()

I feel like your confusion is based purely on the wording of the question - the term "file object input" can certainly be confusing if you haven't worked with Python I/O before. In this case, the "file object" is infile and the "input" would be the rawdata file, I suppose.

Sign up to request clarification or add additional context in comments.

5 Comments

Right, which is what I was thinking it would be except that doesn't use input, which the instructions specifically says to use.
It says to use "the file object input", but the only method that returns a file object is open()
so yeah. despite the instructions specifically telling me to use input, I'm not supposed to use input. and just as a bit of irony, the code you put up was literally what I originally came up with but then tried to figure out how to use input to address the assignment. So I really could have just saved myself 2 hours of frustration by hitting submit on my original code.
Yeah, it might be worth mentioning something to your instructor about the ambiguity of the problem statement. Especially if this is an introductory course
I don't know this for a fact but from what I've heard from the instructors, none of them are happy with the changes MPL made this past year and they are just counting down the days until the contract expires so they can not renew it. Lord knows I am.
-1

Currently taking this class and figured this out. This is my contribution to all of us college peeps just trying to make it through, lol. MPL accepts this answer.

input = open('rawdata','r')
datum = int(input.readline())
input.close()

3 Comments

Since input() is a built-in function in Python 3 (note: this was raw_input() in Python 2), redefining input in this way is not a good idea.
Totally get it, you are so right, I just wanted to give the answer that worked for me since we are dealing with an isolated somewhat buggy and badly documented program meant to help people learn but confusing them in the process. I'd tried 50 different variations and once something flew, I wasn't going to ask questions. My Programming Lab is terrible. Your warning is valid.
Well, I suppose that's a fair point and I guess it is a correct answer in the very limited context of MPL. The problem then is that leaving MPL-specific “solutions” on StackOverflow permits their poor training to infect the broader Python community. That's not your fault, though, but it is something which future readers ought to be aware of.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.