I know that this question has been asked before, but none of the solutions given there seemed to work for me. As the title suggests, I wrote a battleship game in python on Windows and it worked there, but when I tried to run it inside the Linux terminal, I'm getting an error I never got on Windows. Here's the code.
It loads up normally, but when I input the starting position it says NameError: name 'a0' is not defined for example. I tried putting !/usr/bin/python at the beginning, but nothing changed. I'm sorry if this is a duplicate or if I'm missing something really obvious, but I would really appreciate some help. Thanks in advance.
The error seems to be in the way you are accepting input from terminal. For accepting textual data (a0, R, L, U, D etc... ) you need to use
raw_input()
in place of input. Just replace input() with raw_input() and it will work on all platforms.
Related
I've been working on a project lately which requires me to get a value stored in a text file. Simple task, right?
I have tried pretty much every stupid solution that uses readline() at some point, but when it's printed, there's nothing. Debug also tells me that it is empty.
Since I've experienced some inconsistencies with python already, I tried using the same function inside a different python file, and running it does exactly what it's supposed to do.
My current solution (not the best, but currently I just want it to run properly) is
count_file = "count.txt"
filehandle = open(count_file, 'r')
line = filehandle.readline()
print(line)
which works fine when used in a file called "test.py". When used in my "main.py", it returns nothing. And yes, I used it on the same context and temporarily deleted everything else from the main file while testing.
Does anyone have a clue what causes this? I could just paste the other stuff into the test file and rename it, but 1. that's annoying and 2. it'd be useful to know how to avoid this.
EDIT: I am not certain yet, but I think that the problem is caused by my IDE (am using the latest version of Pycharm Professional). I solved the problem by deleting and re-adding the Run/Debug Configuration.
Sometimes you will get return value as None if you do not specify the file path. please check the path/relative path of "count.txt" file with respect to "main.py" script.
First point: your file path is relative, so it will be resolved according to the current working directory, NOT depending on your script's location.
Second point: you read a single line, it can indeed be empty or only contains non-printable characters.
Third point: run your code without pycharm. Pycharm is probably fine when you really understand what it does behind your back, but if you don't you will indeed experiment some unexpected results. Just using the command line and a simple code editor is actually much simpler (though possibly not as productive) and less confusing.
And finally, don't forget you have an interactive Python shell. Quite handy for testing out things...
About 95% of the time I input something into my console (function of something, defined object, etc.) I don't get an output, instead, I get 3 dots, with a colon, as if it wants me to input something.
Often times I can just spam the variable into the console, and eventually instead of '...:', I'll get 'Out [###]:(thing I called)', but this is absolutely ridiculous.
I've never had this problem in 2.7. I've always used Spyder as my IDE.
I've done my research on here, asked around, but cannot solve this.
Thanks in advance :)
Screenshot of my problem
Another shot
I figured out the solution a couple months ago, I figured I would post it here for those who may be experiencing the same issue. Simply press Shift+Enter instead of just Enter, when inputting into Console. Stupidly simple solution. Thank you all again.
The python interpreter is expecting additional lines of code. Either you typed the wrong command or you made a mistake in what you typed. This will happen, for example, if you type part of a command and hit return. The interpreter is expecting the rest of the command.
I am trying to hot swap a file in python. I am creating a game that takes a really long time to load. But I don't want to reload it every time. I am trying to change some code while the programme is in runtime.
For example:
I want to change this:
while True:
print("Hello")
to this while in runtime:
while True:
print("Hello World")
I looked hot swapping up for python and all of them are answers that I am not looking for. All the other answers change modules. I want to change the current file. Like java in eclipse. Please help!
I want to change the current file. Like java in eclipse.
When you modify Java code in Eclipse, the code is not currently running. In reality, Eclipse has a Java compiler built-in, which attempts to compile your code as you type it. That's how Eclipse is able to give you such fast feedback about syntax errors and type errors in your code. But it can't give you any information about the runtime behaviour of your code (whether or not it produces the right answer), because it doesn't run the code! You need to press the Run button for that.
So I think the question you really want to ask is not "Can I dynamically modify Python code?" (the answer to that is "Yes, but it's complicated and has caveats and is not a good idea") but "Does there exist a Python IDE which can give me feedback about syntax errors while I type?" The answer to that is an emphatic "Yes!". There's an extremely comprehensive list of options in this answer.
You can use dynamic execution using exec function.
Store the code you want to execute in a string and change it during runtime.
So I have been messing around with python with openCV the last couple of weeks and it has worked perfectly. My problem is when I took two desperate code script's and put them together it wouldn't compile. (I know Duh python white space (I fixed all of it,"I think")) It threw an error in the terminal with a line number. At this time I was using text editor in Ubuntu so I threw it into Geany to figure out what line. When I got there I couldn't pick out what the error was all of the indentation was that of the original code and it fitted with that of the rest of the loop it was nested in. So I tried to compile it in Geany and it didn't throw errors at all. I find this extremely weird because Geany is only an editor and it relies on outside compilers to compile the code. I am assuming that the terminal is using the same compilers too (though I know it must not now). I though maybe it has something to do with the library openCV because I am not including it in Geany. So I changed the variable name that was throwing things. After that it still threw the same error so I came here confused.
The error message I am getting is
http://imgur.com/1otMyeZ
My code is at
http://pastebin.com/HYKjnyyc
The part that is giving error is here
http://pastebin.com/6TyXs3uc
Your problem still goes back to the white space you were working on. Everything (except line 297 to the end) below:
# THE DEMO PROGRAM STARTS HERE
is indented, including your three global variables, which makes them appear to the compiler as part of the class Target, which takes the variables out of the global scope, so they are not defined. Remove the unnecessary indentation in that area.
EDIT:
Looking back at your code again, You may also have some indentation problems from the
# Function Definitions
at line 88 and down. From the way the functions are indented they appear to be part of the class, but from the way they are called they appear not to be.
You would find writing in python a lot less frustrating if you use a more standard approach to your indentation/white-space handling. Read this PEP for some best practices in that regard.
I am developing using PyDev in Eclipse. I have put in some comments in my code. I get wavy red lines underneath certain words. The program runs fine and there are no warnings mentioned. So what is the meaning of these wavy lines.
e.g.
#!/usr/bin/python - I get the line under usr and python
# generated by accessing registry using another script written in VBScript.
# The scripts can do the follwing things.
- I get wavy lines under the words registry and following.
I need these comments as I may run the module on its own later.
Thanks for the help.
Neil speaks the truth, except for telling you to turn it off -- spell check in comments is quite helpful -- those who come after will thank you for ensuring they can read your comments without trying to decode random spelling errors.
The lines are simply pointing out words your IDE thinks are spelled wrong. I don't know why it doesn't understand "registry", but you have, in fact, misspelled "following" (as "follwing"). Fix the latter, ignore the former (or add it to the dictionary, don't remember if there's a convenient mechanism for that There is! See Macke's helpful comment below.).
Might be this is just a spellchecker. You have a typo "follwing" instead of "following".