SyntaxError: invalid character in identifier [closed] - python

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
When executing with Python it shows error:
return (x * (1.0 — x))
^
SyntaxError: invalid character in identifier
How do I correct it?

Use the correct character for you minus operator: -. You are using some other 'dash' character that the interpreter is considering just a name like y or x. But it is invalid!
>>> bad_minus = "—"
>>> good_minus = "-"
>>> bad_minus == good_minus
False
>>> ord(good_minus)
45
>>> ord(bad_minus)
8212
>>>

Assuming the character between 1.0 and x is supposed to be a minus sign, replace it with an actual minus sign.

Your minus is not a minus. It's a "em dash".
Try replacing this '—' with '-'.

Related

Python - List function inserting values invalid syntax [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
I am new here learning Python. I have a problem of syntax but I am unable to fix it. Can someone please help ?
Here's the code:
def saisie(the_list)
input_string = input("Enter a list elements separated by space (max 6)\n")
the_list = input_string.split()
if len(the_list) > 6:
return '{}Error'
return '{}Error'
else:
return f'user list: {the_list}'
saisie(liste)
and I got this error :
File "c:/Users/Jay/Desktop/Python Exercices/app/tribulle.py", line 6
def saisie(the_list)
^
SyntaxError: invalid syntax
Which is the invalid syntax, what should I change please?
When defining a function, you must put a colon at the end of the "def saisie(the_list)" or else Python will not know that you are defining a function.
You are missing the colon, it should be
def saisie(the_list):

F strings giving syntax error in python 3.7.6 [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 code:
a = 10
b = 20
print(f "The variable a is {a} and the variable b is {b}.")
returns this error:
File "main.py", line 3
print (f "The variable a is {a} and the variable b is {b}")
^
SyntaxError: invalid syntax
The version I'm using is after 3.6, so it should work. I'm using Anaconda's prompt, if that's a problem.
you have a space between f and the string. Remove it and everything will work.
The syntax error points to the end of the string. If it pointed to the beginning of the string it would give you a better hint at the problem: after the f, which is interpreted as a variable in this case, a string is unexpected.
You need this:
a = 1
b = 2
test_str = f"{'The variable a is'} {a} {'and the variable b is '}{b}"
print(test_str)
Between the curly braces, string interpolation takes place. The variables for a and b are within two of the curly brace sets and hard-coded strings are within the other two.

Understanding "invalid decimal literal" [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 3 years ago.
Improve this question
100_year = date.today().year - age + 100
^
SyntaxError: invalid decimal literal
I'm trying to understand what the problem is.
Python identifiers can not start with a number.
The 'arrow' points to year because underscore is a valid thousands separator in Python >= 3.6, so 100_000 is a valid integer literal.
Sadly in python you cant make variables that start with numbers so therefore you could use something like this:
hundred_year = date.today().year - age + 100

Python Regex stops after first "|" match [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
p = re.compile("[AG].{2}[ATG|ATA|AAG].{1}G")
regex_result = p.search('ZZZAXXATGXGZZZ')
regex_result.group()
'AXXATG'
I was expecting AXXATGXG instead.
Use a grouping construct (...) rather than a character class [...] around the alternatives:
p = re.compile("[AG].{2}(?:ATG|ATA|AAG).G")
^^^^^^^^^^^^^^^
The (?:ATG|ATA|AAG) matches 3 sequences: either a ATG, or ATA or AAG. The [ATG|ATA|AAG] character class matches 1 char, either A, T, G or |.
Note the {1} is redundant and can be removed.
Python:
import re
p = re.compile("[AG].{2}(?:ATG|ATA|AAG).G")
regex_result = p.search('ZZZAXXATGXGZZZ')
print(regex_result.group())
# => AXXATGXG
See IDEONE demo

Python string equality always returns false [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
I've got a problem with my Python code. For some reason a comparison of two strings is always returning False.
def checkChanged(checkURL, currentMessage):
tempMessage = urllib.urlopen(checkURL)
tempMessage = tempMessage.read()
print (tempMessage + ". " + currentMessage + ".")
if (str(tempMessage) == str(currentMessage)):
print ("equal")
return False
else:
print ("not equal")
return True
(Assume indentation is correct. I had to re-format when inserting here)
The problem I think is the if statement, I have tried many variations where both string weren't enclosed by the str(), I have also tried is instead of == but it is False. I have printed both values on the line before just to check and they are infact equal. Am I missing something?

Categories