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?
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 3 years ago.
Improve this question
def kaka(name):
r=''
for ch in name:
r=r+ch*3
return r
Output:
>>> kaka('Mississippi')
>>> 'MMMiiissssssiiissssssiiippppppiii'
But for this code:
def kaka(name):
for ch in name:
r=''
r=r+ch*3
return r
I am getting output as: iii
That's because in your second code you're re-assigning r back to the empty string ''. Thus you only get the final character multiplied 3 times (which for Mississippi is i).
You are getting 2 different outputs because in the first code you are initialising the value of r i.e r = '' outside the for loop and in the second program you are initialising value of r inside the for loop.
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 4 years ago.
Improve this question
print = ('Tell me about your pet. ')
about_pet = input()
if 'dog'.lower() in about_pet == True :
print('ah, a dog')
if 'cat'.lower() in about_pet == True :
print ('ooh, a kitty')
print ('Thanks for the story')
when I run this code I get an error:
str object is not callable
What causes this?
print = ('Tell me about your pet. ') overwrites the function print as a string. It's not longer a function after that, so any time you try to call it as a function later, you're going to get errors.
Get rid of the = so you aren't changing what print is.
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 4 years ago.
Improve this question
phone_letters =[" ", "1", "ABC","DEF","GHI","JKL","MNO","PQRS","TUV","WXYZ","*","0","#" ]
key = 0
string = input("Enter a Letter: ",)
while key < 10 :
if string in phone_letters[key]:
print(key)
return key
else:
key = key+1
return "not found"
I am getting error 'return' outside function; I checked indentation and still the error continues.
return may only occur syntactically nested in a function definition
Source: https://docs.python.org/3/reference/simple_stmts.html#the-return-statement
Your return is not nested inside a function. Therefore, the error.
You can use print("not found") if you want to display the text.
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
A problem exists in Python 2.7.11, with the print function:
elif e=="randomize w and x":
random=randint(int(w),int(x))
print random
elif e=="randomize w and y":
random=randint(int(w,int(y))
print random
The boldfaced print shows up as a syntax error, yet all 278 others in my program do not. Why this is, and how I fix it?
The problem is that in
random=randint(int(w,int(y))
a close parenthesis after w is missing, therefore Python thinks the expression continues on next line, but print at that point is a syntax error.
Your problem is not with the print statement, rather the line right before it. The line before hass inbalanced parenthesis:
random=randint(int(w,int(y))
Make sure you balance them out (add an extra ) at the end), and your error on the next line will disappear.
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
I'm new to python so this may be a beginner question. My 'else' statement in the code below gets a syntax error for a reason beyond my mind. I've looked up the syntax for it multiple times but I cannot find the error. This is my python code:
siteswap = input("Enter the siteswap you want to validate here:")
aantal_digits = len(siteswap)
i = 0
j = 1
while i != aantal_digits:
if (int(siteswap[i])+ (i + 1)) % aantal_digits == (int(siteswap[1:aantal_digits])+ (j + 1)) % aantal_digits:
print("This siteswap is invalid")
break
elif i != aantal_digits:
del (int(siteswap[i])
else:
print ("This siteswap is valid")
break
The else is highlighted and I get a "syntax error".
Your problem is
del (int(siteswap[i])
You are missing a closing parenthesis (but the parenthesis are unnecessary in the first place). Also, del int(siteswap[i]) will not work, because you cannot delete function calls: SyntaxError: can't delete function call
del siteswap[i]
will delete the actual item from your array.