Python syntax error with " [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Hello I'm a noob and just started using python so I wanted to have fun using raw_input , adding random questions and stuff but I came across an error. It says the invalid syntax is ". I've been trying to put it to different places but didnt succeed I'm sorry if this is a really dumb question but can someone help me out.
Heres a line with the error
print " Oh okay then %s . You came from %s , right? Oh i see you came here to %s" % (name,so,fun)
It's pointing at the closing " next to %s
I'm using atom
edit: heres the error
File "C:\Users\USER\AppData\Local\atom\app-1.26.1\Testing.py", line 10
print " Oh okay then %s . You came from %s , right? Oh i see you came here %s. " % (name, so, fun)
^
SyntaxError: invalid syntax
Heres the full code :
name = raw_input("Hello whats your name?")
so = raw_input("From where did you come from")
fun = raw_input("Interesting, why did you come here?")
#start the sentence with "for"
print " Oh okay then %s . You came from %s , right? Oh i see you came here %s. " % (name, so, fun)
If I can't use raw_input what can I use then? I learned with using that.

I fixed the issue by putting brackets around the whole thing like
print (" Oh okay then %s . You came from %s , right? Oh i see you came here to %s" % (name,so,fun))
Also I believe that raw_input was replaced with 'input()'.

Related

Need help making statistics calulator [closed]

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 12 months ago.
Improve this question
Currently working on statistics calculator but an error message saying invalid syntax which points at print in the mode section
import statistics
amountOfNumbers = input("How many numbers are you using? ")
usersNumbers = input("What are your numbers? ")
print("Mean: " , statistics.mean(usersNumbers)
print("Mode: " , statistics.mode(usersNumbers))
print("Median: " , statistics.median(usersNumbers))
Error message reads:
File "D:\Luke's Coding stuff\Python\bot1.py", line 5
print("Mode: " , statistics.mode(usersNumbers))
^
SyntaxError: invalid syntax
the problem is on the line before the problem,you need put this:
print("Mean: " , statistics.mean(usersNumbers))
you forgot the parenthesis, I hope it helps you.
The problem is actually in the line above it, you missed a bracket.
print("Mean: " , statistics.mean(usersNumbers))
The reason it told you there was a syntax error in line 5 is because it expected the code to follow the print function's "value" syntax.
You are missing a closing parentheses after your first print statement.

Why is python giving me an error for improper indentation when my indentation is correct? [closed]

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
This is the error message I got:
File "main.py", line 15
while True:
^
IndentationError: unindent does not match any outer indentation level
This is my full code:
import wikipedia
from colorama import Fore, Style, Back
y = input("tell me what you want ")
z = int(input("how many sentences "))
try:
text = wikipedia.summary(y, sentences=z)
print('')
print("---Text---")
print(text)
print("----------")
print(len(text.split()),"words.")
except:
print(Fore.RED + "ERROR)
while True:
print("\a")
Can you please explain why this is happening? I am using the Repl online ide.
The answer to this is that I was mixing up tabs and spaces. You shouldn't use both because this error can happen.
While coding in python, it's super important to pay attention to the way you indent. Of course you can either use tab or space, but always make sure you stick with one of them and not mixing them together.
Seems like you haven't done it this time. Delete the indentations and reindent them. Should work fine.

Passing a variable to a command - not enough arguments for format string [closed]

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 6 years ago.
Improve this question
hi I have the following block of test code (test lab).
#<snip>
client_mac = 'f8:cf:c5:a4:a2:84'
#<snip>
# issue a command1
my_command_output1 = my_wlc_session.sendcommand("grep include f8:cf:c5:a4:a2:84 \"show client summary\" \n")
# print the output
print(my_command_output1)
#
# issue a command2
my_command_output2 = my_wlc_session.sendcommand("grep include %s \"show client summary\" \n") % client_mac
# print the output
print(my_command_output2)
#<snip>
Command1 works as expected.
But command2 is the issue. I want some way of passing the client_mac to the command, but the code I am using results in this;
TypeError: not enough arguments for format string
I think this is something to do with the /n , but I need a 2nd new-line after the command for it to be executed.
Is there a better way of passing the client_mac? Or am I doing something else wrong.
Your closing parenthesis seems to be in the way. Try:
my_command_output2 = my_wlc_session.sendcommand("grep include %s \"show client summary\" \n" % client_mac)
While you're at it, the new .format() is becoming more popular, and makes bugs like this a little easier to notice. That'd look like:
my_command_output2 = my_wlc_session.sendcommand("grep include {} \"show client summary\" \n".format(client_mac))
Your code problem is here:
("grep include %s \"show client summary\" \n") % client_mac
Your % client_mac is supposed to be inside the parenthesis with the string you are formatting it into ("grep include %s \"show client summary\" \n").
The error means you specified a format string (%s) but did not include the variable to insert (because it was outside the parenthesis, and therefore, not part of the expression).
Re-reading your question, I believe the source of your confusion is that you thought you were passing multiple arguments to your function. You are actually just passing a single argument, a string (which you are formatting to include the client_mac).

I don't understand how to fix this print indentation error [closed]

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 6 years ago.
Improve this question
name = str(input("Whats your name?: "))
if name is 'Chris':
print("Hello, " + name)
^
IndentationError: expected an indented block
So this error shows up right beneath the "t" in print. I have tried putting a space after it, like so:
print ("Hello, " + name)
But that didn't work either. Anyone know what I should be doing differently?
I am using this in the Python command window as opposed to Sublime which I usually use, because it doesn't seem to read the input that is typed in.
Do it like this:
name = str(input("Whats your name?: "))
if name == 'Chris':
print("Hello, " + name)
You had an extra " mark after name. Also, you want == not is; is is for determining if two objects are identical, meaning, the literal same object.
Unless you're doing something more advanced, you'll typically only use is to compare with None:
if variable is None:
print("No value provided for variable!")
Also, make sure that you use the same number of spaces to indent each block of code. If you use 4 spaces, and 3 spaces somewhere else, you'll get an IndentationError.

Python, syntax error using "if, elif" [closed]

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 new to Python and while trying to write a scrip that will keep asking questions about the user until the scrip gets FALSE,
I decided to check the scrip,of course it gave me an syntax error that told me the mistake was on the fifth lane, `a.
Now on that lane I tried to change the old value of a to a new value.
sadly, I can't understand the mistake that I made, can some one please check it and explain me what went wrong ?
#!/usr/bin/python
print "Hello, I'm wilfred and I'm an Artificial Intelligence\n"
a=str(raw_input("Do you want to be my friend? \n"))
if a=="yes":
a=str(raw_input("Yey ! my first friend,what is your name?\n"))
if a==str :
print "Nice name man!"
elif a==int :
print "bye!"
elif a=="no":
print "Well, nice to meet you anway, good bye now \n"
Your line
a=str(raw_input("Yey ! my first friend,what is your name?\n")
Indent this line so it is inside the 'if' statement
Add a ')' at the end of this line
You just need to indent the line. Your code should work fine. Keep learning python. It's awesome!!!!
#!/usr/bin/python
print "Hello, I'm wilfred and I'm an Artificial Intelligence\n"
a=str(raw_input("Do you want to be my friend? \n"))
if a=="yes":
a=str(raw_input("Yey ! my first friend,what is your name?\n"))
if a==str :
print "Nice name man!"
elif a==int :
print "bye!"
elif a=="no":
print "Well, nice to meet you anway, good bye now \n"
To further help with the test cases, I changed your string and int tests for you. "==" test is for value btw.
#!/usr/bin/python
print "Hello, I'm wilfred and I'm an Artificial Intelligence\n"
a=str(raw_input("Do you want to be my friend? \n"))
if a=="yes":
a=str(raw_input("Yey ! my first friend,what is your name?\n"))
if a.isalpha() :
print "Nice name man!"
elif a.isdigit() :
print "bye!"
elif a=="no":
print "Well, nice to meet you anway, good bye now \n"
A general structure for this kind of repeating loop is
while True:
a=str(raw_input(...))
if a=="whatever": break
# other responses to a

Categories