Why is it saying my variable is undefinded? - cs50

Hey guys i am new to programming and currently going through CS50 on edx. I am having trouble printing my variable and am getting an error saying it is undefined. I have defined this variable in my for loop on line 22. Can i not print a variable if it is defined in a "for" loop? Below is a screenshot with the error and the variable defined in the link.Any advice is greatly appreciated! Thanks in advance! Error in Question

Your variable have scope inside the for loop only, so if you need to print your variable years, try to use print inside for loop.
for (int years;.....){
printf(years)
startsize = ......
}

Your variable is undefined because it is only defined in the scope of the for() loop. Between the {}'s. You need to move the definition of "int years" outside the for() declaration for it to "live" longer.

Related

Unable to modify a fucntion from inside the function

I am trying to change variable (state) from fun3 which is called from multiple function as shown in the image.
When i print state from inside fun3, it prints the modified variable game.But when i print state from func1, it prints menu.
Problem: it is not modifying the orignal variable in the file game.py.
I spent 3 hours to solve the problem but it is not solving.
If any one has the solution Please help in this problem.

Python: why can a variable stand on its own?

I am a beginner in python told my friend today, that the following code would throw an error, but it did not:
a = 5
a
So I wondered, what does "a" actually do and why is the interpreter fine with this?
If this is a duplicate, please refer me to the right post and sorry in advance.
edit: I used a *.py file.
If you type this code into the shell and click enter, the value of a is returned. Functionally, as there is no operation being performed on a, the value of a will not change.
You define the variable in the line above. The variable contains a value, so the "NameError: name 'a' is not defined" error is not triggered.
Also, even if the variable is a different data type, for example, a string, the value of a is returned.
If you run the code in a different environment, the line won't be printed and the line won't impact the value of itself or of any other variables.
I think you tried it in der REPL Console, paste it to a *.py file and execute that. So when you just type the variable name and hit enter this is actually a print command behind the scenes
You can type in a int into the shell or whatever, and it will return it. The variable you put is just a int, so it returns 5.

python keeps saying that 'imput is undefined. how do I fix this?

Please help me with this. I'd really appreciate it. I have tried alot of things but nothing is working, Please suggest any ideas you have.
This is what it keeps saying:
name = imput('hello')
NameError: name 'imput' is not defined
You misspelled input as imput. imput() is not a function that python recognizes - thus, it assumes it's the name of some variable, searches for wherever that variable was declared, and finds nothing. So it says "this is undefined" and raises an error.
imput isn't a built-in function. Perhaps you're looking for input.

Read statement in fortran

I am trying to understand a small read function in my program I am trying to decipher.The code is below
READ (LREST, END=350, ERR=350) ICHR
IF (ICHR .EQ. ICKLNK) THEN
DO L = 1, 4
READ (LREST, END=350, ERR=350)
ENDDO
So basically LREST is some kind of an argument provided for this subroutine this function is in. However, I found that LREST is not defined anywhere(used grep to see where LREST is defined in my *.f files. So my questions is what is that LREST doing there in READ function. I thought the location LREST is at is where the unit is defined.
Second questions is that ICHR is a 16 character string variable define for this subroutine. However, the contents of ICHR have not been assigned. I have no idea what this READ function is trying to read from.
Going over to IF statement, ICKLNK is another 16 character string variable with a defined strings. Because ICHR is not defined, does that mean this if statement never gets entered in?
Finally, the do loop( or for loop) has variable L in it but it is not even being used for read function inside of the loop.
Im a beginning in fortran so I may just be lacking a very basic knowledge but if you know an answer to my question please let me know. Thanks!
Han
You are correct that LREST is specifying the unit number (or internal file if it is character). You seem to suggest that LREST is an argument in this subroutine or function, which means its value is passed in by whoever calls the function. Showing us only a small piece of the code makes it hard to provide further details.
Again, you say ICHR is an argument to the procedure, so it takes on the value of whatever was passed in by the call. ICLNK is probably similar, but you didn't show all the code.
The DO (not for) loop is using L just as a counter; it doesn't need to be referenced inside the loop.

Can't call Range() method due to undefined value [duplicate]

I am executing a Perl file. I am getting this error. Can anybody please suggest the solution. I am getting this kind of error on many pages like:
"Can't call method "goto" on an undefined value at " ..
Means error is same just by replacing get method name.
Please help.
It means the variable on which the method get is called is undefined instead of being an object that accepts the method.
Look at the line number given by the error message (in the file given by the message). If the error is in a module, and you don't see where the variable should have been initialized, then put use diagnostics; at the top of the script after your use strict; and use warnings; so you get a stack of the method/function calls at the point of error.

Resources