Line of Python code not working in my game - python

So this line of code in my Python game is not working:
direction=raw_input("What would you like to do?\n")
It's supposed to get the player to type in a command either: North, South, East, West, Look, Search, Commands or Inventory. It's coming up with this:
Traceback (most recent call last):
File "/Users/khalilismail/Desktop/COMPUTING/Text-based Games/DragonQuest.py", line 173, in
direction=raw_input("What would you like to do?\n")
EOFError: EOF when reading a line
Please help
Here is the stack of code surrounding this:
while game==on:
while place==town:
direction=raw_input("What would you like to do?\n")
if direction=="west":
if "iron ore" and "wood" and "3 Gold Pieces" in items:
print "The blacksmith greets you, and you tell him that you have the items and money he requires, you also give him the saw to make up for some of the difference, he then forges you a battleaxe and wishes you luck on the rest of your quest"
items.remove ("saw")
items.remove ("3 Gold Pieces")
items.remove ("iron ore")
items.remove ("wood")
items.append ("battleaxe")

As suggested in the comments, some editors don't support input (including Atom). Here are some options you can overcome this:
Set the direction variable by hard-coding it while debugging (don't forget to remove it after debugging)
Change your editor (keep in mind that Sublime also has the same problem, but this has a workaround - Sublime Text 2 console input; Notepad++ however doesn't have this problem when running code through command line).
You could also try and remove the newline '\n' from the raw_input string to test if it works.

Related

Unexpected Indentation in Visual Studio Code with Python

the following code
accounts = f.get_sepa_accounts()
for account in accounts:
print(account)
is throwing the following error:
>>> print(account)
File "<stdin>", line 1
print(account)
^
IndentationError: unexpected indent
I just moved to Visual Studio Code, so no idea if this is a problem specific to the application. I tried 4 spaces and a tab already..Doesn't do anything.
Update:- The more recent versions of VSCode don't seem to have this issue.
You have probably used tabs when indenting. You can check this by selecting the code. When selected the tabs are highlighted with a right pointing arrow in VS Code as shown in the image below:-
Notice the two arrows in the second line. See how the second line is error because of this.
You fix this by selecting the two tabs and pressing Ctrl+H. Replace with the right amount of spaces.
In short replace tabs with spaces.
Press F1 and start typing 'Indentation to spaces' and you will be able to find
'Convert Indentation to Spaces' option, select and click enter.
Next time, mostly you just have to press F1 and enter because it is selected by default since you used it recently.
Restart Visual Studio Code.
Sometimes this can help.
Also ensure that you choose the right linter (CTRL + SHIFT + P > "Python: Select Linter" and choose f.e. pylint) before you restart.

Text Position In Python Shell

I'm looking to make a game in python's Shell. However, when text is printed, it is printed at the bottom. Is there any code I can use to scroll it all to the top or indent it. Please let me know! I'll show an example of what I want to happen:
The code sections are the Shell screens
EDIT
This 'Hello Welcome' is printed at the top of the Shell screen
Hello
Welcome!
This is an example for a UI, the text is indented:
Money: $5
The text is indented
I also want to select a line to print the text eg:
Line 1
Line 2
Line 3
print 'Hi' line 2
If anyone knows how to do this please let me know, I've looked for some time and haven't found any answer.
Use formatting for indentation
print(' %s' % 'hello')
>>> hello
The perfect solution and, as I know common for console apps: clean output and reprint it with updated state

How to fix this invalid syntax?

I'm trying to make a game in python, and stack trace keeps coming up with 'invalid syntax'(those are the EXACT words of the stack trace, not me summing it up) at this line:
print('Look(1) | Bust down door with weapon(2)')
The cursor is always between the 'p' and the 'o' whenever I check it with ALT+X, regardless of where it
was before I checked it.
There are no syntax errors in that line that I can recognize. I was wondering if someone more experienced could help me?
These are the lines above and below that in case you think it's not that line itself that's causing the
problem:
print('You are in a dark and grimy dungeon. You see no windows, and a door. What\
would you like to do?')
print('Look(1) | Bust down door with weapon(2)')
act_umpteenth = input('>>> ')
while act_umpteenth:
[rest of code continues on here that I'm not bothered to copy and paste]
Edit: Solution found. The culprit was a close bracket behind a string that I was assigning to a variable a few lines up. I think I thought it was a print statement or something. :P I deleted it, and now the code works 100%. Thanks for suggesting that I look over the code for indentation/tab/spaces problems, even if it wasn't the problem, because otherwise I wouldn't have spotted the actual problem.
I would just go and redo all your tabs. You can highlight all the code, and press Shift + Tab (you might have to do this multiple times) to back everything up to the left edge. Then you'll want to re-tab everything over in the proper way.
try running it with
python -tt myscript.py
you are probably mixing tabs and spaces when you run it with the -tt command it will tell you about inconsistent indentation
fixing your indentation should resolve your issue
I figured I would put this as an answer even though it is really a comment

Beginner Python book tutorial issue

Please ignore the example, it is just in a book I am currently learning from.
I am running this in Netbeans 6.9.1 as 7 doesn't support Python :( and I am getting a error when trying to run it in the output console. The code is exact as to what is written in the text book. The only thing I can think of is that net beans only supports 2.7.1 yet the book I am learning from is Python 3.1. Could this be the issue? Please let me know if I have overlooked something.
Here is the basic script;
# Word Problems
# Demonstrates numbers and math
print("If a 2000 pound pregnant hippo gives birth to a 100 pound calf,");
print("but then eats 50 pounds of food, how much does she weigh?");
input("Press the enter key to find out.");
print("2000 - 100 + 50 =", 2000 - 100 + 50);
input("\n\nPress the enter key to exit");
Traceback (most recent call last):
File "/Users/Steve/Desktop/NewPythonProject/src/newpythonproject.py", line 6, in <module>
input("Press the enter key to find out.");
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
-Thanks guys.
The issue is that input() means something different in Python 3.x. In Python 2.x, the equivalent function is raw_input().
Simply replace your calls to input() with ones to raw_input() and it will work as expected:
# Word Problems
# Demonstrates numbers and math
print("If a 2000 pound pregnant hippo gives birth to a 100 pound calf,")
print("but then eats 50 pounds of food, how much does she weigh?")
raw_input("Press the enter key to find out.")
print("2000 - 100 + 50 =", 2000 - 100 + 50)
raw_input("\n\nPress the enter key to exit")
The reason this caused a problem is that in Python 2.x, input() takes user text, then interprets it as a Python expression. As you were giving it a blank line, which is an invalid expression, it throws an exception.
I would highly suggest using a different editor if you are learning Python 3.x. PyCharm is great (albeit not free), and Eclipse+Pydev is out there. To be honest, you don't really need an IDE for Python - a good text editor like Gedit that supports code highlighting is all you really need.
Also note I removed the semicolons, which are entirely redundant in Python.

vim highlighting everything in red

I added a print line to a python script while the script was executing, and now all the text is highlighted in red when I open the file. Opening and closing the file doesn't get rid of it. Opening a second python file momentarily fixed the problem, but then closing file and reopening brought the problem back. Now it wont go away at all. Any body know what could cause this?
This happens sometimes in vim when it goes to highlight syntax on multi-line comments. Occasionally everything after the multi-line comment also becomes colored the same as the comment.
I am unsure whether this is a legit bug in vim, or in the actual syntax settings for Python (e.g. python.vim), but I definitely experience this from time-to-time.
For a quick fix, you might try typing:
:hi Error NONE
And then hit Enter.
Old thread, but hope this helps.
By mistake I did a "/." on my vim screen, which highlighted all lines in red. If I open any other file, the red highlighting stays.
Try searching for some other keyword, let's say "/word" - doesn't matter word exists or not. It restores the color.
You probably have an unterminated multiline string. They start and end with either three single or three double quotes.
''' <== this is the start of a multiline string
this is still in the string
this is the last line '''
As per this article http://vim.wikia.com/wiki/Fix_syntax_highlighting, I mapped F12 to resync the syntax highlighting from the start of the file. It has worked better in some instances where <ctrl-l> didn't.
noremap <F12> <Esc>:syntax sync fromstart<CR>
inoremap <F12> <C-o>:syntax sync fromstart<CR>

Categories

Resources