Login [Register]
Don't have an account? Register now to chat, post, use our tools, and much more.
Since there will probably be a lot of these I decided to just make a single thread for it.
My first one is: Does anyone know of a tutorial on classes or have a good explanation of how they work/how to use them? I sort of get the idea, but if someone said "Make a class with _ and make objects with _ categories and access and change the values," I would be completely lost.
Your question doesn't really make sense to me, so for the moment I will just link to the relevant section in the language reference.

Quote:
Make a class with _ and make objects with _ categories and access and change the values
I don't understand what this is supposed to mean. If you can clarify, I expect examples if not better explanation can follow.

Trying to make sense of it, I suspect your sense of how classes and objects relate is skewed: a class is like the template for an object; define a class, and instantiate the class to make objects of that type.

Code:
# Foo is a class, with one method that isn't very useful.
class Foo(object):
    def bar(self):
        return 0

# Create an object 'x', which is an instance of Foo
x = Foo()
assert isinstance(x, Foo)
# Do a thing calling Foo's bar method
assert x.bar() == 0
I think when he says "categories" he means to say "fields."
By categories, I mean the different pieces of data for each object in the class.
EDIT: We covered it in class. However, I am still unclear about how method parameters work and what they are for.
I think I got parameters now but I have a serious issue:
How does one install pygame on a mac? (10.11.6) I can't get anything to work. Does anyone know how or know someone who does?
Hard to provide any kind of useful advice if you don't say what didn't work. In general, Homebrew or Macports are good (relatively easy) choices.
Currently I am having problems getting homebrew installed. All of the instructions I find require this to happen:
Code:
xcode-select --install

but is just says "xcode-select: command not found.
https://stackoverflow.com/questions/20795866/xcode-select-not-found#20795908

(You probably need to install Xcode from the app store.)
Hmmmm. I have the latest version of Xcode installed. When I try
Code:
ls -a
It says "ls: command not found"

Also,

Code:
open -a textedit .bash_profile
returns "open: command not found"
You don't have your path set up correctly. This might be some use:

http://apple.stackexchange.com/questions/11745/reset-your-path-variable

Hope this helps Smile
The two things I mentioned in my last post behave the same. However,
Code:
ruby -e $(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)
now waits a second and then returns "-e:1:in `<main>': undefined local variable or method `' for main:Object (NameError)" After that, the brew related commands just say "brew: command not found"
Quote:

You pasted smart quotes, which are awful and invalid syntax. You need plain quotes (here's one to copy/paste: ").

So:

Code:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

You can rather conveniently tell there's a difference due to how the forum is doing syntax highlighting here.
I am getting the same result as before even with normal quotes.

-bash: curl: command not found
-bash: ruby: command not found

I also restarted my computer but it didn't seem to fix anything.

These are the steps I am using to install pygame for mac if it's helpful.

EDIT: I talked to Mateo for a while and he reminded me to reset my PATH variable. After doing so, the line now outputs "ruby: invalid option -1 (-h will show valid options) (RuntimeError)"
I am currently working on fixing ruby.

ANOTHER EDIT: Ruby won't install either. I might just use the school computers and be done with it.
I covered this in the other thread, but just to clarify, the problem was not having admin access.

Now I have a problem with getting an image to rotate:

Code:
degrees = 0
while True:
    keys=pygame.key.get_pressed()
    if keys[K_w]:
            if shipY > 90:
                inU += 0.1*speed
                inD -= 0.05*speed
            if 180 > degrees > 0:
                degrees -= 2
            elif 360 > degrees > 180:
                degrees += 2

    if keys[K_s]:
            if shipY < 652:
                inD += 0.1*speed
                inU -= 0.05*speed
            if 180 > degrees > 0:
                degrees += 2
            elif 360 > degrees > 180:
                degrees -= 2

    if keys[K_a]:
            if shipX > 1:
                inL += 0.1*speed
                inR -= 0.05*speed
            if 270 > degrees > 90:
                degrees -= 2
            elif degrees > 90 or degrees > 270:
                degrees += 2
                if degrees > 359:
                    degrees = 0

    if keys[K_d]:
            if shipX < 1255:
                inR += 0.1*speed
                inL -= 0.05*speed
            if 270 > degrees > 90:
                degrees += 2
            elif degrees > 90 or degrees > 270:
                degrees -= 2
                if degrees > 1:
                    degrees = 359

When I press keys, nothing happens. I have gone through and checked what it seems like should happen, and it doesn't. The degrees go counter-clockwise starting at the top (0) and ending right to the right of it (359).

There are no errors, just nothing happens. I left out all of the variable initiation here.



EDIT:
I feel kind of stupid right now. I hadn't realised that my degrees were becoming negative and I shoukd have just used else statements. Here is the corrected code:

Code:
degrees = 0
while True:
    keys=pygame.key.get_pressed()

    if keys[K_w]:
            if shipY > 90:
                inU += 0.1*speed
                inD -= 0.05*speed
            if 180 > degrees > 0:
                degrees -= 2
            elif 360 > degrees > 180:
                degrees += 2

    if keys[K_s]:
            if shipY < 652:
                inD += 0.1*speed
                inU -= 0.05*speed
            if 180 > degrees > 0:
                degrees += 2
            elif 360 > degrees > 180:
                degrees -= 2

    if keys[K_a]:
            if shipX > 1:
                inL += 0.1*speed
                inR -= 0.05*speed
            if 270 > degrees > 90:
                degrees -= 2
            else:
                degrees += 2
                if degrees > 359:
                    degrees = 0

    if keys[K_d]:
            if shipX < 1255:
                inR += 0.1*speed
                inL -= 0.05*speed
            if 270 > degrees > 90:
                degrees += 2
            else:
                degrees -= 2
i was bored earlier so i finished these bits off on the bus, they are for the hp39gii
and can be modified for other purposes,
the one thing of many that the hp39gii lacks is an input string command, so i decided to have
a go at making one, and here it is, if your are using cut and paste via the emulator you may
need to reconfirm some of the characters such as the STO> triangle which sometimes gets exchanged for something similar,


Code:

EXPORT InpStr(X,Y,Z,W)
BEGIN
IFERR L1(Z) THEN ""►L1(Z);END;
RECT_P(X,Y,X+220,Y+14,2);
TEXTOUT_P(L1(Z)+"■",X,Y,W,0);
0►C;
WHILE C<95 DO
−1►N;
WHILE (N<16 OR N>50) DO
GETKEY►N;
IFTE(N==36 OR N==41,0►N);
IFTE(N==47,0►N);
IFTE(N==48,0►N);
IFTE(N==49,0►N);
END;
IFTE(N==45,−17►N);
IFTE(N==20,−48►N);
IFTE(N>19,N-1►N);
IFTE(N>35,N-1►N);
IFTE(N>39,N-1►N);
N+49►C;
IF C==1 THEN
mid(L1(Z),1,dim(L1(Z))-1)►L1(Z);
ELSE
IFTE(dim(L1(Z))<16,L1(Z)+{char(C)}►L1(Z));
END;
RECT_P(X,Y,X+220,Y+14,2);
TEXTOUT_P(L1(Z)+"■",X,Y,W,0);
END;
IFTE(dim(L1(Z))<16,mid(L1(Z),1,dim(L1(Z))-1)►L1(Z));
RECT_P(X,Y,X+220,Y+14,2);
TEXTOUT_P(L1(Z),X,Y,W,0);
RETURN(L1(Z));
END;


the above procedure uses X and Y, for positioning on the screen, Z is for the sub part of L1, and the W is for font, 0 use system font, 1 small and 2 large,


Code:

EXPORT Name()
BEGIN
2►W;
RECT_P(3);
TEXTOUT_P("ENTER ITEM NAME",13,10,W,0);
InpStr(13,25,1,W);
TEXTOUT_P("ENTER COLOUR",13,40,W,0);
InpStr(13,55,2,W);
TEXTOUT_P("ENTER SIZE OF ITEM",13,70,W,0);
InpStr(13,85,3,W);
TEXTOUT_P("the "+L1(3)+" "+L1(1)+" is "+L1(2),13,110,W,0);
FREEZE;
END;


anyway not got much time to spare so i will be posting some time in the future,
i didn't want to create a new section just for some bits and bobs...
Ok, here we go again.

I have been working with reading and writing to files, and am mystified by this
Code:
os.path.isfile(os.path.dirname(os.path.realpath(__file__))+'/data.txt'))

This returns true, meaning that data.txt is in fact a file and it is in the same folder (TestFolder) as my program (test.py).

Now I write "Hello World" in the file:

Code:
datafile = open('data.txt', 'w')
datafile.write("Hello World")
datafile.close()


After this, I can verify that data.txt does in fact contain the words "Hello World".

Here is the problem: Whenever I try to print the contents along with another string, it isas if the contents were and empty string. On their own or after being stored into a variable and then printed along with another string, they print fine.

This:

Code:
datafile = open('data.txt')
print(datafile.read())
print("Hi "+datafile.read())
datafile.close()

datafile = open('data.txt')
contents = datafile.read()
datafile.close()

print(contents)
print("Hi "+contents)

Outputs this:

Code:
Hello World
Hi
Hello World
Hi Hello World

Why is there nothing after "Hi" on the second line?
I'm not sure, so please someone correct me if I'm wrong, but it maybe possible that Python works like PHP, where .read() reads the file until it his an enter or it reaches the end of the file. I guess it has an internal pointer or so, such that the first .read() reads the first line (correct), but then the second .read() continues from the previous pointer, which is already at the end of the file, so it will be empty. Again, I'm not sure, but this is my only guess Wink
Calling read with no parameters reads to the end of the file, at which point the stream position remains at the end so further reads will read nothing (unless you also append somewhere else).

Consider:
Code:
with open('data.txt') as datafile:
    print(datafile.read())
    datafile.seek(0)
    print("Hi "+datafile.read())
Yes; .read() returns the file's contents from wherever your last .read() finished, to the end of the file. You can use .seek(0) to go back to the beginning of the file.

.read() takes an argument size, and will read that many bytes. If you do this in a loop, you can read a file one chunk at a time. This is why having that internal pointer is useful.
Oh, I see. Thanks everyone!
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are GMT - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement