Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have entered the following code:
print("The new value for x is: ",x)
and the value of x is 9.
I expected that it will display something like: The new value for x is:9
but it displayed:
('The new value for x is: ', 9)
How can I fix that? also, my python version is:2.7.13
In python 2.7 you do not use braces when calling print.
The correct syntax for printing a string with a parameter concatenated to it in python 2.7 would be:
print "The new value for x is: " + str(x)
The syntax you are using is the python 3 syntax.
Note
As was stated in the comments by #ArpitSolanki, the code you used actually creates a tuple in python 2.7, and prints the result, hence you see the braces in the output.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am using Python 3 and Pycharm 2021.3. I want to print "4" on the first line. And "3" on the second line. I wrote code like this:
a="4"
b="3"
print(a\n,b)
It show a Error message. Please tell me the right way to write this. Thanks in Advance.
print(a + "\n" + b)
Or
print(a)
print(b)
Or
print(a, b, sep="\n")
When you want to explicitly add a new line, you should use "\n", like you did. However, you mustn't forget that this is a character, so you need to surround it by " or '.
Next, when you want to concatenate string variables, the standard is the use of +.
Put together, if you want to concatenate three variables, in which one of them is a string, a possible way would be:
final = a + '\n' + b
print(final)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm continuing learning python and I want to use the value of a variable as a key in a dictionary, and the input of the user as the value of that key, but what I'm doing is doing the opposite.
def scan_option(option_type):
option_dict={}
elected_type = option_type + '_option'
option_dict.update(elected_type= raw_input("Enter the {} Option: ".format(option_type)))
return option_dict
print scan_option("Hardrive")
In case user answered 8:
The output is: {'elected_type': '8'}
and I want it to be: {'Hardrive_option': '8'}
What do I need to change in the code to do that?
update takes a dictionary as its argument. The stuff you fed it was a symbol and a string; it converts that to a dictionary and then does the update.
The syntax you need here is a simple dictionary assignment:
option_dict[elected_type] = raw_input(...)
You might try simpler commands when you first write your code. Once those work properly, then you can go for the one-line, compound commands.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm trying to make a simple console game in Python 2.7, where the user gets a dilemma and makes a decision by typing the number correlating to the option he wants to choose. This question is the main menu.
s = raw_input("Enter a number in the range 1 to %s\n> " % v
if (is_number(s) and s in q):
return s
I'm getting a "SyntaxError: Invalid syntax" from the if (is_number(s) and s in q) statemet when trying to run the program. It was working fine before i added the question.
This is my first real program.
You're missing a parentheses on the previous line:
s = raw_input("Enter a number in the range 1 to %s\n> " % v
# here--^
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Issue occurred right after I added the and x= "J1234": I don't know what's going on.
original = ""
if len(original) > 0 and x = "J123":
raw_input("Enter a word:")
original = raw_input('word')
print "original"
else:
print "empty"
x.isalpha()
So, you're getting a SyntaxError. To fix this, change:
x = "J123":
to...
x == "J123":
In Python the former is for setting values and the latter is for checking them. Therefore when you use this the wrong way round the syntax is incorrect.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to write a quicksort program in python, however I'm getting an invalid syntax error at else statement in the second last line below:
import random
n=int(raw_input("Enter the size of the list: ")) # size of the list
intlist = [0]*n
for num in range(n):
intlist[num]=random.randint(0,10*n)
pivot=random.choice(intlist)
list_1=[] # list of elements smaller than pivot
list_2=[] # list of elements greater than pivot
for num in range(n):
if num<=pivot:
list_1.append(num)
else
list_2.append(num)
This is not a complete program as I am still writing.
add a colon after the else so that it looks like else:. and pick up a good tutorial ;)
Looks like you need a ':' after "else".