print "Question?",
answer = raw_input()
The error:
Brians-Air:PythonFiles Ghost$ python ex11.py
File "ex11.py", line 1
print "How old are you?
^
SyntaxError: EOL while scanning string literal
I removed the "," and the interpreter gave an error. My thought was that removing the "," would give a new-line and request input on this new-line.
My question is why is the "," after the print statement necessary? Is this just the syntax coded into Python?
here is what you need to write:
while True:
print 'Question?'
answer = raw_input(' >')
if answer == ('done'):
break
print answer
Related
This question already has answers here:
Differences between `input` and `raw_input` [duplicate]
(3 answers)
Closed 3 years ago.
I get a name error even though I'm just trying to put a string into a variable.
I am trying to do this on Python 2.7.11. Does anyone have anything that helps? Upgrading Python is not an option for me.
def translate(phrase):
translation = ""
for letter in phrase:
if letter in " ":
translation = translation + "#"
result = (input("enter a phrase you want encrypted: "))
result = translate(result)
This is the error that's shown:
Traceback (most recent call last):
File "D:\hello\encrydecry\encryption1.py", line 158, in <module
>
result = (input("enter a phrase you want encrypted: "))
File "<string>", line 1, in <module>
NameError: name 'hello' is not defined
In Python 2, when you use input(), Python interprets the input. So when you type hello, hello is interpreted as a variable, and you're essentially doing result = hello. Hence the error NameError: name 'hello' is not defined.
One option is to simply type the input between quotes, so it will be interpreted as a string: 'hello'.
To avoid the input being interpreted altogether, you have to use raw_input() instead of input(), which doesn't interpret the user input and always returns a string:
result = raw_input("enter a phrase you want encrypted: ")
def translate(phrase):
translation = ""
for letter in phrase:
if letter in " ":
translation = translation + "#"
result =raw_input("enter a phrase you want encrypted: ")
result = translate(result)
Hey I have the following code:
#node.route('/txions')
def transactions():
txions_str = ""
for txion in this_nodes_transactions:
txions_str + "FROM: %s \n TO: %s \n AMOUNT: %d \n" % (txion['from'], txion['to'], txion['amount'])
return txions_str
I get my Python linter complaining that the line is too long for txions_str, what is the correct way to format this line for when using mulitple parameters?
First, it's worth pointing out that you are returning an empty string there...
Anyways, you already have line breaks.
So, break your code to accommodate them
#node.route('/txions')
def transactions():
txions = []
for txion in this_nodes_transactions:
txions.append("FROM: {} ".format(txion['from']))
txions.append(" TO: {} ".format(txion['to']))
txions.append(" AMOUNT: {} ".format(txion['amount']))
return '\n'.join(txions)
Python also supports multi-line strings and line-continuation characters, but those dont seem needed here.
I am new in python. I trying do some list with name and I don't know where is in here problem
>imiona = ["zbychu", "rychu", "zdzisiu"]
>print (imiona)
>imiona[1] = "rychu2"
>print (imiona[1])
>print ((len(imiona))
>imiona.append("Renata")
>print (imiona)
and the error showing:
imiona.append("Renata")
^
SyntaxError: invalid syntax
your line:
print ((len(imiona))
has three opening parenthesis '(' and only two closing ')'
Try this, looks like you have missing/extra parenthesis:
def main():
imiona = ["zbychu", "rychu", "zdzisiu"]
print(imiona)
imiona[1] = "rychu2"
print(imiona[1])
print(len(imiona))
imiona.append("Renata")
print (imiona)
main()
Why is the output forcing a new line on the result when there isn't a \n after print (message_2.capitalize())?
# Input example python script
# Apparently in python 3.6, inputs can take strings instead of only raw values back in v2.7
message_0 = "good morning!"
message_1 = "please enter something for my input() value:"
message_2 = "the number you entered is ... "
message_3 = "ok, now this time enter value for my raw_input() value:"
message_final1 = "program ended."
message_final2 = "thank you!"
print ("\n\n")
print (message_0.capitalize() + "\n")
input_num = input(message_1.capitalize())
print ("\n")
# This line is obsoleted in python 3.6. raw_input() is renamed to input() .
# raw_input_num = raw_input(message_3.capitalize())
# data conversion
print ("Converting input_num() variable to float...\n")
input_num = float(input_num)
print ("\n")
print (message_2.capitalize())
print (input_num)
print ("\n")
print (message_final1.capitalize() + " " + message_final2.capitalize())
Output is as follows:
Good morning!
Please enter something for my input() value:67.3
Converting input_num() variable to float...
The number you entered is ...
67.3
Program ended. Thank you!
print(), by default, will add a newline. So the two statements:
print (message_2.capitalize())
print (input_num)
will put a newline in between the message and the number.
Either pass in both objects to print to one print() call:
print(message_2.capitalize(), input_num)
or tell print() not to add a newline by setting the end argument to an empty string:
print(message_2.capitalize(), end='')
print(input_num)
Python's print() function's default behavior is to print a newline follwing the input string.
You can change this behavior by adding the optional parameter end:
print("My message", end="")
I've asked my friends around, and rather than just deleting my question I thought to add value to this site by sharing the answer :
# , operator here removes an unexpected newline. the print statement has a built in newline. That's why!
print (message_2.capitalize(),(input_num),"\n")
# Alternative way to display the answer
print (message_2.capitalize() + " " + str(input_num))
So the key is to use a comma operand or do a string operator on the answer.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to print in Python without newline or space?
How to print a string without including ā\nā in Python
I have a code that looks like this:
print 'Going to %s'%href
try:
self.opener.open(self.url+href)
print 'OK'
When I execute it I get two lines obviously:
Going to mysite.php
OK
But want is :
Going to mysite.php OK
>>> def test():
... print 'let\'s',
... pass
... print 'party'
...
>>> test()
let's party
>>>
for your example:
# note the comma at the end
print 'Going to %s' % href,
try:
self.opener.open(self.url+href)
print 'OK'
except:
print 'ERROR'
the comma at the end of the print statement instructs to not add a '\n' newline character.
I assumed this question was for python 2.x because print is used as a statement. For python 3 you need to specify end='' to the print function call:
# note the comma at the end
print('Going to %s' % href, end='')
try:
self.opener.open(self.url+href)
print(' OK')
except:
print(' ERROR')
In python3 you have to set the end argument (that defaults to \n) to empty string:
print('hello', end='')
http://docs.python.org/py3k/library/functions.html#print
Use comma at the end of your first print: -
print 'Going to %s'%href,