if/elif/else statement in a while loop with python [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 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.

Related

Python: if condition for an another function result? [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 4 years ago.
Improve this question
Solved: Thanks for your helps. i'm working dictionary and hash codes in one page, and there was a block problem. i thought, my logic is wrong but i checked all page, and i fixed blocks. its works fine right now.
Here's my problem:
d = {}
d['a'] = 'alpha'
d['b'] = 'beta'
missing_key = 'x' in d
if missing_key == True:
print ("The key, you are looking for was successfully found!")
else:
print ("The key, you are looking for was not found!")
print ("Here are keys in database:")
for k, r in enumerate(d.keys(), start=1)
print ("Key {}: {}".format(k, r))
that for condition works perfectly. but i cant run that if condition. Where am i doing wrong ? Thanks for your help.
Getting this error:
File "C:/Python/dictionary-hash.py", line 33
if missing_key == True:
IndentationError: unexpected indent
also i'm using "Python 3.6" and "Anaconda Spyder"
There's nothing wrong with the logic in your code. Since the error is an IndentationError, it must be because you're using inconsistent indentation on that if line. Make sure that the indentation on each line is consistent (using same number of spaces/tabs). Probably there is a space at the beginning of that line that is causing the error.

How to resolve "return" outside function 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 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.

If statement checking if a variable is equal to a specific word is getting 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 6 years ago.
Improve this question
So, I'm trying to make an easter egg in my calculator program, where if you type my friend johns name in, it prints nerd, but I keep getting invalid syntax on the line the if statement starts. here's what the code looks like:
x = input(' ')
if x = John:
print nerd
else:
print x
please keep in mind i'm using python 2.7, not 3. when I googled it, I only got answers that worked in 3.
x = raw_input('enter name ')
if x == 'John':
print 'nerd'
else:
print x
You are doing an assignment, but you need == to check for equality

Python indentation error in if statement [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 have a following trouble in Python.
sister_age = 15
brother_age = 12
if sisiter_age > brother_age:
print "sisiter is older"
else:
Error:
SyntaxError: invalid syntax
if sisiter_age > brother_age:
print "sisiter is older"
else:
File "<pyshell#5>", line 4
else:
^
IndentationError: unindent does not match any outer indentation level
No matter how many times I try to change the index, it shows an error.
Assuming there is something after the else statement, the else statement should be aligned with the if statement, not inside it.
Example -
if sisiter_age > brother_age:
print "sisiter is older"
else:
Also, if you do not have (or need) anything inside the else statement, you should just remove it.

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