Syntax error when using __init__ [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 apologize if the answer is obvious, but for some reason, the following code gives me a syntax error at every line after def _ _ init _ _ block.
If I simply comment out init, the entire program works fine however.
import graphics
from graphics import *
class Block:
def __init__(self,x,y,win,fid,length,orien,colour):
wind = win
if (orien == "horizontal"):
topL,dump = getPoint(fid)
lightRect = Rectangle(getPoint(fid)
# else:
# x =5
def draw(self):
return
def undraw(self):
return
def highlight(self):
return
def unhighlight(self):
return
def switchHighlight(self):
return
def move(self,target):
return
Any help would be greatly appreciated!

Missing ")" in:
lightRect = Rectangle(getPoint(fid)

Related

Beginner python3 question about elif statement syntax and the use of print_function [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
Trying to build a simple script for returning disk, cpu usage and network connectivity checks. I keep getting errors in my elif statement syntax. I've tried revising this for 3 days and finally am not getting errors in the str format line 36 but now i get an invalid syntax error on line 37 (line containing only "else:". I read that in earlier python versions you had to import print_function, which is why it is listed, though I believe you shouldn't need to do this in Python3. Any suggestions or insight into why this error is occurring would be very much appreciated.
#!/usr/bin/env python3
import shutil
import psutil
import print_function
from network import *
def dsk_free(disk):
df = shutil.disk_usage(disk)
fre = df.free / df.total * 100
dsk_out = 'Disk usage = {}%'.format(fre)
return (dsk_out)
def cpu_ok():
cpuse = psutil.cpu_percent(1)
cpu_out = 'Cpu usage = {}%'.format(cpuse)
return (cpu_out)
def check_disk_usage(disk):
du = shutil.disk_usage(disk)
free = du.free / du.total * 100
return free > 20
def check_cpu_usage():
usage = psutil.cpu_percent(1)
return usage < 75
x = dsk_free
y = cpu_ok
if not check_disk_usage("/") or not check_cpu_usage():
print("ERROR! Shut down immediately!")
elif check_connectivity() and check_localhost():
print("Everything is awesome! Disk Usage is {}% and Cpu usage is {}%".format(dsk_free(),cpu_ok())
else:
print("Network connection failure!")
if not check_disk_usage("/") or not check_cpu_usage():
print("ERROR! Shut down immediately!")
elif check_connectivity() and check_localhost():
print("Everything is awesome! Disk Usage is {}% and Cpu usage is {}%".format(dsk_free(),cpu_ok())
else:
print("Network connection failure!")
in your second elif statement, where you have your print statement, you are missing the finishing ), add one more ) at the end and you should be good to go

Yes / No Function Always Returning 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 2 years ago.
Improve this question
I'm completely new to programming and am trying to write a function that takes a yes or no question. However, when I run the below it always seems to complete with my variable as False. There has to be something simple here that I'm missing. I'd love to hear any thoughts / feedback / improvements to it. Thank you!
def yes_or_no(question):
answer = input(question).lower().strip()
print("")
while not(answer == "y" or answer == "yes" or \
answer == "n" or answer == "no"):
print("\nSorry, only Y or N please.")
answer = input(question).lower().strip()
print("")
print(answer)
if answer == 'y' or answer == 'yes':
answer = True
else:
answer = False
You forgot to return the answer, so that function returns None by default. Add return answer at the end of the function.

Python Scope and Nonlocal Issues [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 a little confused with the whole nonlocal thing. Here's the problem snippet of code:
positon = 0b0
while True:
pos_choice = input("\tPlease enter your choice: ").lower()
if pos_choice == '1':
position = position | Baseball.pitcher
break
elif pos_choice == '2':
position = position | Baseball.catcher
break
elif (pos_choice == 'd') and (position != 0b0):
break
elif (pos_choice == 'd') and (position == 0b0):
print("\tChoose a position.")
else:
print("Invalid choice.")
print(position)
So this throws me :
Traceback (most recent call last):
File "driver.py", line 252, in <module>
load_student()
File "driver.py", line 142, in load_student
position = position | Baseball.catcher
UnboundLocalError: local variable 'position' referenced before assignment
Based upon what I've read on answers to other questions, the problem would be because the problematic "position" is nested two loops in from the original call (is that right?).
My main problem is that I can't figure out how to bind the two "position"s using nonlocal, though I have tried various solutions to no avail. Also, is using nonlocal a taboo like it is when using global? Thanks for the help!
You have a typo in your code. In the declaration, the variable is spelled positon instead of position.
There is a simple typo in your first line.

class and functions in python [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 8 years ago.
Improve this question
I'm new to Python and I'm trying classes and objects, I have this script:
#!/usr/bin/env python
class test:
def __init__(self, username):
self.username = username
def name_again(self):
for i in range(0-4):
print ("username is %s" %self.username)
ahmed = test('ahmbor')
ahmed.name_again()
I'm expecting this script to print "username is ahmbor" 5 times
When I run this script, I have nothing
Please help find what's wrong with this
You are telling range() to loop over 0-4 (subtract four from zero), which is -4. Because the default is to start at 0 and count up, that is an empty range:
>>> range(0-4)
range(0, -4)
>>> len(range(0-4))
0
Use a comma instead, and use 5 to loop 5 times, not 4. The endpoint is not included:
>>> len(range(0, 4))
4
>>> len(range(0, 5))
5

How do you return object from "__main__" python function? [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 9 years ago.
Improve this question
def returnSomeObject(File):
. . .
SomeObject = loader.load(File)
return SomeObject
if __name__ == "__main__":
return returnSomeObject(sys.argv[1])
When trying to return an object from the "__main__" python function, I get:
SyntaxError: 'return' outside function
if __name__ == "__main__":
return returnSomeObject(sys.argv[1])
This is because __name__ == '__main__' is an if statement, and a return statement can only exist within a function.
So more like
def main():
return returnSomeObject(sys.argv[1])
if __name__ == '__main__':
main()
But you need not even do that, you can just do:
if __name__ == '__main__':
returnSomeObject(sys.argv[1])
Note that this will print nothing to the console.
turns out the file I was calling it from was the problem location, I needed to Replace
import returnSomeObject as returnSomeObject
with:
from returnSomeObject import returnSomeObject

Categories