Text Position In Python Shell - python

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

Related

Line of Python code not working in my game

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.

Python IDLE shell - How to make multiline strings

ANSWERED (But if for some reason you want to read it you can)
I am a beginner at Python and I know how to type the correct syntax but when I repeat it, it comes out with no extra lines, it just says "\n"
DETAILS ON EXAMPLE BELOW:
My variable is fred and I set it to what it says below, and then on the 3rd line I made it print the text.
Please tell me if I didn't describe it correctly, keep in mind I'm a beginner.
EXAMPLE:
>>> fred = '''How do dinosaurs pay their bills?
With tyrannosaurus checks!'''
>>> fred
'How do dinosaurs pay their bills?\nWith tyrannosaurus checks!'
The \n in that string represents the new line symbol. For pretty much all purposes, it is a new line.
What you're doing is correct.
To print the lines on separate lines:
for line in jack.split("\n"):
print(line)

How to include a newline in the python raw_input() function?

I'm using raw_input() to storing a message inside a variable. So I can't press enter for a carriage return/new line to start a new paragraph. Right now if I press enter it will just proceed to the next portion of my program.
I already tried something like this:
>>> message = raw_input("Message: ")
Message: Hello Sir, \n It's great that..
>>> message
"Hello Sir, \\n It's great that.."
>>>
It didn't worked, and I also tried enclosing it in single and double quotes, which also didn't worked.
I understand that there are other ways of doing this, like using wxpython or tkinter, but I want to keep it strictly console. Is this possible?
Can you use the sys module? This will do the trick if you want. Just hit Ctrl-D to end it.
import sys
message = sys.stdin.readlines()
Otherwise, this answers your question: Python raw_input ignore newline

How to debug indentation errors in python

I am trying to write my very first python script. This was working but then after some slight refactoring I have, apparently, broken the indentation. I can not determine what is the problem. The interpretor complains about the following method. Can someone point it out?
def dataReceived(self, data):
a = data.split(':')
print a
if len(a) > 1:
command = a[0]
content = a[1]
msg = ""
if command == "iam":
self.name = content
msg = self.name + " has joined"
elif command == "msg":
msg = self.name + ": " + content
print msg
The error reads: File "python_server.py", line 17
a = data.split(':')
^ IndentationError: expected an indented block
I encountered a similar problem using Sublime Text 2.
To solve, click on the "Tab Size" at the bottom of the editor, and choose "Convert Indentation to Tabs".
You start using a text editor that allows you to show indents, and you become consistent about using spaces instead of tabs, and you enforce that in your editor.
There are a great number of things you can do here:
Use an editor that can show control characters (like vi with set list).
Use a hex dumper program like od -xcb.
Just delete the white space at the start of that line and re-insert it (may want to check the preceding line as well).
if you're using the "Sublime Text 2" editor, then I found this answer helpful - it details how to turn on whitespace characters and also convert tabs to whitespaces
sublime-text-2-view-whitespace-characters
Try Editra - www.editra.org
Your code looks fine, syntax seems fine...your text editor may be creating your errors. Review your file with Editra to see/review indentation levels.
Editra saved my sanity - I thought I had correct syntax when viewing my script in Text Editors including Notepad++ with python indent plugin. However, when I would run the script, it would throw off indentation errors every time. I finally opened the script up in Editra, and I could see the problem. Notepad++ and other text editors did not show correct indentations/tabs/spaces. Editra showed errors e.g. unexpected spaces, tabs - which I was able to correct.
Editra will auto-indent your script [and show errors -tabs, spaces -that may not show up in other text editors].
If you have indent errors it will show up as blue underlined segment;
If you are writing script [adding/deleting lines] Editra will auto-indent the script.
**I would suggest opening your script and editing it in Editra.
Hope this helps!
Best of luck.
str8arrow

Problem with reading pasted text in terminal

I'm reading text in terminal with
description = raw_input()
It works if I write the text and press enter. The problem is when I paste the text from somewhere with Ctrl+Shift+V or with right click + paste. My program immediately ends, description contains only part of the text (I can see it in database).
Do you know how to do this so paste works? I'm using xfce4-terminal in Ubuntu.
thank you
Make sure that your pasted text doesn't contain any embedded control characters (such as a newline), which could end the input.
Try this, lifted it from a google search. You have to enter 3 blank lines after your paste, but of course, if there are 3 blank lines in your paste, then it won't work. The concept is there though, you just need to pick an end control character. Thanks for the comments catching that guys.
print "paste quote:"
emptycount = 0
lines = []
while emptycount < 2:
t = raw_input()
if len(t) == 0:
emptycount +=1
else:
emptycount=0
lines.append(t)
lines.append("\n")
print " ".join(lines[:-1])

Categories

Resources