Anyone know how to fix illegal target for anotation [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 28 days ago.
Improve this question
was making some code and i ran into the error here is the code
import random
foo = ['sad', 'mad', 'happy ', 'inbetween', 'bored']
print("hello answer 1 or 2 or 3")
e = input(str)
if e == '1' : print("ok give me a sentence and ill tell you how i feel about it") ;a = input("please enter a sentence ") ;a == ('i hate you') :print(" i feel sad and mad that you hate me") ;a == ('I hate you') :print("i feel sad and mad that you hate me")
elif e == '2' :print("ok ill give you advice")
elif e == '3' :print("so you want to have a converstation")
else :print(" thats not an option")

A minimal reproducible example for this:
>>> if True: True: print('x')
...
File "<stdin>", line 1
SyntaxError: illegal target for annotation
The problem is that the second True was intended to be another if condition, but is instead just an expression. The parser treats : print('x') as a type annotation for True; but type annotations have to be applied to individual variable names, not expressions.
The specific error is only possible because print('x') is an expression, and thus a valid type annotation. If it were another kind of statement, this would cause a generic syntax error:
>>> if True: True: pass
File "<stdin>", line 1
if True: True: pass
^
SyntaxError: invalid syntax
Either way, there was a typo here - the second True: was meant to be if True:. However, this does not fix the problem by itself:
>>> if True: if True: pass
File "<stdin>", line 1
if True: if True: pass
^
SyntaxError: invalid syntax
It is not permitted to put multiple ifs on the same line; nor can elif and else be paired with the matching if on the same line:
>>> if True: pass; else: pass
File "<stdin>", line 1
if True: pass; else: pass
^
SyntaxError: invalid syntax
Putting multiple statements on a line - by using semicolons, and/or by putting the "suite" of an if etc. statement on the same line - is strongly discouraged in Python. It has multiple limitations, and makes the code much harder to read.
To make the example work, use indented blocks in the normal way:
if True:
if True:
pass

Related

Why can't an if be used as statement to another if on the same line?

An ìf works fine as single line statement, like this:
if True: print('OK')
However, if that is given as statement to another single line if, it generates a syntax error, as for:
if True: if True: print('Breaks with "SyntaxError: invalid syntax"')
Why is this construction a syntax error?
As jarmod pointed, the reference by else clause would not be clear in:
if cond_1: if cond_2: print('if part')
else: print('else part, would that be for cond_1 or cond_2 ?')

Syntax error on else: statement in code + idk why it keeps saying I have a bad title

So, my problem is I need to put mondayAssign right where I have it cause it appears it will just ask the question everywhere else and I don't want it too so I have to do this and I have an error on elif: any one wanna help?
__import__("replit").clear()
whatReturn = "monday"
f = open('hold.txt', 'r')
g = f.seek(0)
h = f.readlines()
for line in h:
pass
def monday():
if whatReturn in line:
mondayAssign = input("Assign a task for monday: ")
else:
print("error has occured.")
monday()
def mondayy():
if whatReturn in line:
mondayAdd = input("Anything else you would like to add, 'yes' or 'no': ")
mondayAdd == 'yes'
mondayDoubleAssign = input("Assign a task for monday: ")
elif:
print("Error!!!")
else:
mondayAssign = input("Assign a task for monday: ")
#the last else is just a place to store the mondayAssign cause it appears I cant put it anywhere else without it just printing itself and I don't want to ask same question twice
mondayy()
print(mondayAssign, mondayDoubleAssign)
It's very simple, you use the unconditional elif construct. Either add some condition or replace elif with else. In the event that you want to handle the exception, you need to use the try except construction.
Here is a variant using "else":
__import__("replit").clear()
whatReturn = "monday"
f = open('hold.txt', 'r')
g = f.seek(0)
h = f.readlines()
for line in h:
pass # This is extra code
def monday(h):
for line in h:
if whatReturn in line:
mondayAssign = input("Assign a task for monday: ") # to use "line" you must first define it
else:
print("error has occured.")
monday(h)
def mondayy(h):
for line in h:
if whatReturn in line:
mondayAdd = input("Anything else you would like to add, 'yes' or 'no': ")
mondayAdd == 'yes' #it's not very clear why there is a comparison
mondayDoubleAssign = input("Assign a task for monday: ")
elif some_condition:
print("Error!!!")
else:
mondayAssign = input("Assign a task for monday: ")
#the last else is just a place to store the mondayAssign cause it appears I cant put it anywhere else without it just printing itself and I don't want to ask same question twice
mondayy(h)
print(mondayAssign, mondayDoubleAssign)
If you look at the code in general, I think you should get a better understanding of loops and conditional statements, since you seem to have problems with them. Do not grab onto everything at once :) It is better to first study each of the topics in detail, and then try to combine them. This also applies to functions, by the way. Here's a little scribbled on using it all:
#lets begin from loops
#simple usage:
for some_variable in some_sequence:
print(some_variable) # some variable access only in loop it means that you can use it only in loop body(4 spaces)
#now briefly about conditional operators:
if some_statement:
print("something")
elif some_other_statement:
print("do something else") # it means optional condition
else:
print("do something different in any way that does not fit the above")
#again, you need to remember about the scope, that is, something will work inside the condition only if it is written after 4 spaces
#now briefly about functions:
#a function is an algorithm that we have named by some name and, as a rule, we use repeatedly. In parentheses, we pass the arguments to the function, that is, external data that needs to be used inside the algorithm.
def simple_func(arg_one, arg_two):
print(arg_one, arg_two)
simple_variable_one = "hello"
simple_variable_two = "world!"
simple_func(simple_variable_one, simple_variable_two) #output: hello world
Good luck learning Python, I hope you succeed!

Newbie query about conditional statements [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
I'm learning Python and can't work out why the following doesn't work.
Can anyone advise? code below
thanks
# Make sure that the_flying_circus() returns True
print "What is your number?"
num = input()
print "What is bla?"
bla = input()
def the_flying_circus():
if num ==4 and bla=="bla": # Start coding here!
return True
print "Well done!"
# Don't forget to indent
# the code inside this block!
elif num == 2 or bla== zog:
print "OK"
# Keep going here.
# You'll want to add the else statement, too!
else:
print "Bad luck!"
the_flying_circus()
The return True is probably not what you want to have on the top of the if block. Try removing it.
The only condition that will return True is num==4 and bla=='bla'. Otherwise, the return value is None. However, 'Well done!' will never be printed since the return statement occurs first.
Couple of things...
1) return True should be moved to the end of the function (as mentioned by others)
2) watch how you collect input... use raw_input for your string, use input for the number.
This works for me:
def the_flying_circus():
if a==4 and b=='bla':
print "Well done!"
elif a==2 or b=="zog":
print "OK"
else:
print "Bad luck!"
return 1
a = input("What is your number? ")
b = raw_input("What is bla? ")
the_flying_circus()

Python: Making a Magic 8 Ball game, why do i keep getting syntax errors? Here's the code, test it if you want

Here is my code. When I ask a question I get a SyntaxError: invalid syntax. Any help would be appreciated.
import random
answer1=("Absolutely!")
answer2=("No way Pedro!")
answer3=("Go for it tiger.")
print ("Welcome to the Magic 8 Ball game - use it to answer your questions...")
question = input("Ask me for any advice and I'll help you out. Type in your question and then press enter for an answer.")
print ("shaking....\n"*4)
choice=random.randint(1,3)
if choice == 1:
**strong text**answer==answer1
elif choice == 2:
answer==answer2
else:
answer==answer3
print (answer)
This error comes up when I ask any question
Welcome to the Magic 8 Ball game - use it to answer your questions...
Ask me for any advice and I'll help you out. Type in your question and then press enter for an answer.should i play xbox
Traceback (most recent call last):
File "E:\Magic8Ball.py", line 11, in <module>
question = input("Ask me for any advice and I'll help you out. Type in your question and then press enter for an answer.")
File "<string>", line 1
should i play xbox
^
SyntaxError: invalid syntax
>>>
You are using == which is a comparison operator to assign variables. It should be = instead. Also, you forgot to declare answer: answer = "".
import random
answer1=("Absolutely!")
answer2=("No way Pedro!")
answer3=("Go for it tiger.")
print ("Welcome to the Magic 8 Ball game - use it to answer your questions...")
question = input("Ask me for any advice and I'll help you out. Type in your question and then press enter for an answer.")
print ("shaking....\n"*4)
choice=random.randint(1,3)
answer = ""
if choice == 1:
answer=answer1
elif choice == 2:
answer=answer2
else:
answer=answer3
print (answer)
If you are using Python2.7 use raw_input instead of input

Weird indentation error [Python 3] [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
So I am coding and when I tried to print out a certain line from a text file , it would keep on giving me an indentation error.But when I indent it , it gives me invalid syntax.EDIT= THE CODE WORKS WHEN I REMOVE IF GTIN == 86947367:, BUT WHY ?
if GTIN == 86947367 :
fp = open("read_it")
for i, line in enumerate(fp):
if i == 0:
elif i == 2:
fp.close()
Correct indentation for your code will be:
if GTIN == 86947367:
fp = open("read_it")
for i, line in enumerate(fp):
if i == 0:
pass
elif i == 2:
pass
fp.close()
Make sure you don't mix TAB with spaces as it will ruin your indentation altogether.
The better indentation for your code I think is :
if GTIN == 86947367:
fp = open("read_it")
for i, line in enumerate(fp):
if i == 0:
pass
elif i == 2:
pass
fp.close()
You always need your for loop, and your file closure indented at the same level as your 'open' statement, so that you loop over the file only if you have opened it
An even better solution would be :
if GTIN == 86947367:
with open("read_it") as fp:
for i, line in enumerate(fp):
if i == 0:
pass
elif i == 2:
pass
Using 'with' ensures that your code will always close the file, even if an error occurs. It is good practice to use the 'with' when opening files or simiar - and you don't need to use 'close' as the 'with statement takes care of it automatically.

Categories