Open Side Menu Go to the Top
Register
Programming homework and newbie help thread Programming homework and newbie help thread

11-05-2014 , 05:58 PM
Hi all., I'm having a go at a python 3 class in uni, and I'm having quite the difficulty with it. I figured I better get help here throughout the rest of the year or I'm going to fail. I should have started seeking help here weeks ago, but Skyrim kind of corrupted that whole idea. Thankfully/, I'm finally burned out on Skyrim and am regaining focus on uni.

My last program gave me a hell of a time because I was having issues with the urllib.request library. I kept getting this error when I was trying to split by comma delimiter.

Quote:
Type str doesn't support the buffer API
I looked around on the web and the best I figured, it was a problem with the update from python 2 to 3., and I needed to update the urllib.request library, which I never figured out how to do..

So my first question is, how do I update the urllib.request library? I heard it was a simple download file, drop file into folder type thing, but **** me if I could ever find the damned file.

The assignment for that program was to retrieve data from a webpage and split it into readable format and basically present data the user entered, for what it's worth. I could retrieve the data from the URL, I could print it raw, but I couldn't split it on any delimiter that I tried.
Programming homework and newbie help thread Quote
11-18-2014 , 07:34 PM
i noticed a little while ago that this thread slowed way down, and it seems like the students have a lot fewer questions now than previously. I'm sure this is just the structure of classes, but it's a pretty cool feeling having all the stuff you learned coming together into one neat little package.

Just did a bunch of functions for a class and then tested it. Worked fine, aside from a small logic error on my part. Just amazed at what a rush it is to solve problems and have an ever-growing arsenal with which to approach these problems.

It's really quite a confidence boost to have taken c++, a language I totally failed at much earlier in my life, and to be getting comfortable with it. Can't wait to know enough to do useful things!

Really hope all the other folks who've been posting here are doing well and haven't given up. Never give up! This is too amazing a field to get discouraged out of!
Programming homework and newbie help thread Quote
11-19-2014 , 01:29 AM
It is close to thanksgiving, probably everyone taking tests or studying for them and have no time for forums. Or they all gave up xD
Programming homework and newbie help thread Quote
11-19-2014 , 04:38 AM
silly question about string indexing in python...

when i type:

Code:
s = 'peanutabcdxyzbutter'
x = s[4]
print x
print x[-1]
both print statements return the same value (u). so i don't really understand how the [-1] works...

why is it that the following code works (returns longest alphabetical substring of 's'), but it doesn't work without the [-1] at the end of line 5?

Code:
s = 'peanutabcdxyzbutter'
curString = s[0]
longest = s[0]
for i in range(1, len(s)):
    if s[i] >= curString[-1]:
        curString += s[i]
        if len(curString) > len(longest):
            longest = curString
    else:
        curString = s[i]
print 'Longest alphabetical substring: ' + str(longest)
Programming homework and newbie help thread Quote
11-19-2014 , 05:15 AM
Quote:
Originally Posted by pewpewpew
silly question about string indexing in python...

when i type:

Code:
s = 'peanutabcdxyzbutter'
x = s[4]
print x
print x[-1]
both print statements return the same value (u). so i don't really understand how the [-1] works...
x = s[4] so it contains the single character u. So if you take x[-1] which
is the last character of x you just get u. Did you want to do s[-1]?. That should give r.

Quote:
why is it that the following code works (returns longest alphabetical substring of 's'), but it doesn't work without the [-1] at the end of line 5?

Code:
s = 'peanutabcdxyzbutter'
curString = s[0]
longest = s[0]
for i in range(1, len(s)):
    if s[i] >= curString[-1]:
        curString += s[i]
        if len(curString) > len(longest):
            longest = curString
    else:
        curString = s[i]
print 'Longest alphabetical substring: ' + str(longest)
Here you compare the character s[i] of s and add it to curString if it comes after the last character
in curString (curString[-1]) in the alphabet.

Last edited by Mr.mmmKay; 11-19-2014 at 05:24 AM.
Programming homework and newbie help thread Quote
11-19-2014 , 06:09 AM
sdfafgas

yeah thanks
Programming homework and newbie help thread Quote
11-26-2014 , 09:33 AM
Nm

Last edited by MadTiger; 11-26-2014 at 09:37 AM. Reason: Nm
Programming homework and newbie help thread Quote
11-28-2014 , 08:52 PM
I'm working on a simple python 3 game assignment using Turtle., and I'm a bit stuck. Here's the assignment:








Here's my code:


Code:
import turtle
turtle.setup(300,300)
window = turtle.Screen()
window.title("Manual Snake")
window.bgcolor("lightgreen")
arenaTurtle = turtle.Turtle()
snakeTurtle = turtle.Turtle()
arenaTurtle.color("black")
snakeTurtle.color("purple")
arenaTurtle.penup()
arenaTurtle.goto(-100,100)
arenaTurtle.pendown()
arenaTurtle.speed(0)

def drawArena():
    arenaTurtle.forward(200)
    arenaTurtle.right(90)
    arenaTurtle.forward(200)
    arenaTurtle.right(90)
    arenaTurtle.forward(200)
    arenaTurtle.right(90)
    arenaTurtle.forward(200)


def left():
    snakeTurtle.left(90)
    
def forward():
    snakeTurtle.forward(25)


window.onkey(left,"Left")
window.onkey(forward,"Up")
drawArena()
window.listen()
window.mainloop()


I'm unsure how to do the edge detection functionality. Do I need some sort of if/boolean statement? Like if snakeTurtle x,y coordinates are > something???
Programming homework and newbie help thread Quote
11-28-2014 , 10:52 PM
web design is such a cluster****.

somehow cobbled together several webpages, but having issues with image positions on one page. We've got a single css stylesheet for all the pages, and embedded css for the page with the images.

Alls I need to do, I think, is to slide everything on this one page over about 50 pixels to the left. I assume I need to do it in the embedded styles because of how css works, but I sure as hell can't figure it out.

Oh, maybe I literally just did. You can set negative margins? Sweet!
Programming homework and newbie help thread Quote
11-29-2014 , 12:34 AM

Thanks mate, I finally got it all sorted out. I knew I had the right idea with the booleans. Ending the game after hitting the edge wasn't a requirement, but we could add that if we wanted to so I did.

Here's my final code, which I have two questions about, as I feel like I'm just now starting to get a handle on the basics.


Code:
import turtle
turtle.setup(300,300)
window = turtle.Screen()
window.title("Manual Snake")
window.bgcolor("lightgreen")
arenaTurtle = turtle.Turtle()
snakeTurtle = turtle.Turtle()
arenaTurtle.color("black")
snakeTurtle.color("purple")
arenaTurtle.penup()
arenaTurtle.goto(-100,100)
arenaTurtle.pendown()
arenaTurtle.speed(0)
size = 200   # line 14

def drawArena(size):   # line 16
    for i in range(4):
        arenaTurtle.forward(size)   # line 18
        arenaTurtle.right(90)

def left():
    snakeTurtle.left(90)
    
def forward():
    snakeTurtle.forward(25)
    if snakeTurtle.xcor() >= 100:
        snakeTurtle.penup()
        snakeTurtle.goto(0,0)
        snakeTurtle.pendown()
        snakeTurtle.write("Dead")
        print("Dead")
        window.ontimer(quit, 2500)
    if snakeTurtle.xcor() <= -100:
        snakeTurtle.penup()
        snakeTurtle.goto(0,0)
        snakeTurtle.pendown()
        snakeTurtle.write("Dead")
        print("Dead")
        window.ontimer(quit, 2500)
    if snakeTurtle.ycor() >= 100:
        snakeTurtle.penup()
        snakeTurtle.goto(0,0)
        snakeTurtle.pendown()
        snakeTurtle.write("Dead")
        print("Dead")
        window.ontimer(quit, 2500)
    if snakeTurtle.ycor() <= -100:
        snakeTurtle.penup()
        snakeTurtle.goto(0,0)
        snakeTurtle.pendown()
        snakeTurtle.write("Dead")
        print("Dead")
        window.ontimer(quit, 2500)

def quit():
    window.bye()


drawArena(size)   # line 59
window.onkey(left,"Left")
window.onkey(forward,"Up")
window.onkey(quit, "q")
window.listen()
window.mainloop()

My two questions are:

1. Passing the size of 200 as an argument to the drawArena method was a requirement, did I do that correctly with lines 14, 16, 18 and 59? I commented/bolded the lines for ease of visibility.

2. Is there anything that's blatantly inefficient, or wrong about my code? I improved my drawArena function with a for loop, instead of hard coding the square. I'm unsure if the forward function is excessive with all the if statements, but it works fine so I think it's good. Just want to double check though.
Programming homework and newbie help thread Quote
11-29-2014 , 08:33 PM
Regarding question 2 if there is anything blatant: you've pasted the same 6 lines of code in four places. Learn to avoid that. Either put those lines in a function and reuse that, or combine all those 'ifs' with 'ors' so you just need one block.

My preference would be two functions: if outofbounds(): killPlayer()

Good job on the loop, that's kind of the same idea.
Programming homework and newbie help thread Quote
11-29-2014 , 09:46 PM
Quote:
Originally Posted by catsec
My two questions are:

1. Passing the size of 200 as an argument to the drawArena method was a requirement, did I do that correctly with lines 14, 16, 18 and 59? I commented/bolded the lines for ease of visibility.
The scoping rules in Python can generate some surprises:

Code:
size = 200

def f():
    print(size)

f()

>> 200
but...

Code:
size = 200

def f(size):
    print(size)

f()

>>> Traceback (most recent call last):
 TypeError: f() missing 1 required positional argument: 'size'
So, you have to be careful.

Quote:
2. Is there anything that's blatantly inefficient, or wrong about my code? I improved my drawArena function with a for loop, instead of hard coding the square. I'm unsure if the forward function is excessive with all the if statements, but it works fine so I think it's good. Just want to double check though.
When you find yourself copy/pasting code, you should be thinking about how you can abstract the function. You have the exact same thing in the code. You also have mixed ideas working, seeming to imply forward movement involves death.

I would move all of the copy / paste functions to its own function, like so:
Code:
def dead ():
    snakeTurtle.penup()
    snakeTurtle.goto(0,0)
    snakeTurtle.pendown()
    snakeTurtle.write("Dead")
    print("Dead")
    window.ontimer(quit, 2500)

def forward():
    snakeTurtle.forward(25)
    if snakeTurtle.xcor() >= 100:
        dead()
    if snakeTurtle.xcor() <= -100:
        dead()
    if snakeTurtle.ycor() >= 100:
        dead()
    if snakeTurtle.ycor() <= -100:
        dead()
That reveals more patterns and generalities. As you have it written, you can break it down to something much simpler:

Code:
def dead ():
    snakeTurtle.penup()
    snakeTurtle.goto(0,0)
    snakeTurtle.pendown()
    snakeTurtle.write("Dead")
    print("Dead")
    window.ontimer(quit, 2500)

def forward():
    snakeTurtle.forward(25)
    if abs(snakeTurtle.xcor()) == 100 or abs(snakeTurtle.ycor()) = 100:
        dead()
But I don't really like this either. Why? Because the walls are hard-coded in the function. At this point, you may consider finding the edges. The following tries to demonstrate this (I don't have turtle installed, so nothing is tested, you'll have to debug this yourself):

Code:
TOP = 100
RIGHT = 100
LEFT = -100
RIGHT = -100

def dead ():
    snakeTurtle.penup()
    snakeTurtle.goto(0,0)
    snakeTurtle.pendown()
    snakeTurtle.write("Dead")
    print("Dead")
    window.ontimer(quit, 2500)

def forward():
    snakeTurtle.forward(25)
    if (snakeTurtle.xcor() == RIGHT or
       snakeTurtle.xcor() == LEFT or
       snakeTurtle.ycor() == TOP or
       snakeTurtle.ycor() == BOTTOM):
        dead()

forward()
The CAPS variables means "global" variable in this code. Be sure that you aren't doing mutations in your functions.
Programming homework and newbie help thread Quote
11-29-2014 , 10:47 PM
Allen C,

Shoot, I didn't even see/think of that. Makes sense to make that block of code a separate function, and just call the function, rather than having that same block of code four times. Cheers!


daveT,

I'll have to do some studying over your post. I do have a question though. In general, why is it bad to "hard code"? Scalability?

Edit: Nevermind on the second question, found this.
http://www.tutorialspoint.com/python/number_abs.htm

Last edited by catsec; 11-29-2014 at 11:05 PM.
Programming homework and newbie help thread Quote
11-29-2014 , 11:04 PM
abs() is absolute value.

I'm saying you shouldn't hard-code because you may have that variable across the entire file. Think about what would happen if you had a large file and you wanted to make a change to only the TOP variable and you only had numbers. If you were to f/p 100 with 50, how would you accurately do this without changing every other parameter?
Programming homework and newbie help thread Quote
11-29-2014 , 11:08 PM
I think I follow, but what is f/p?
Programming homework and newbie help thread Quote
11-29-2014 , 11:46 PM
I meant f/r as in find / replace. laziness ftl.
Programming homework and newbie help thread Quote
11-30-2014 , 05:57 PM
You know, catsec, you should probably work through that book I linked. I used an older version of the book in my introductory learning, and I highly recommend it.
Programming homework and newbie help thread Quote
11-30-2014 , 08:53 PM
daveT,

Thanks for your posts and recommendation. I really appreciate them. I've been focused so far on working through the required text for my uni Python class, Python: Programming in context, and have been working through the modules in codecademy.com. There's only two weeks left, and if I do really well on the final exam, I'll pass.

I've also got Learn Python the Hard Way, Violent Python, and Hacking Secret Cyphers with Python, but haven't started to work through them yet. Once I finish this python 3 class in uni I plan to work through them, and I'll definitely work through the book you suggested as well. I don't want to pass this class and then just give up on programming. I really like programming, Python is awesome, and I want to become a decent programmer so I can write hacking tools, and contribute to open source projects, at the very least.

Cheers!
Programming homework and newbie help thread Quote
12-01-2014 , 03:27 PM
Woohoo, the end is in sight!

I have a list of tons of stuff that is due between now and the 11th, then graduation on the 12th and start job on the 15th!!!

Anyone else nearly done?
Programming homework and newbie help thread Quote
12-01-2014 , 03:49 PM
nearly done with first semester, wooo

Grats though, that's pretty amazing!
Programming homework and newbie help thread Quote
12-01-2014 , 04:18 PM
Quote:
Originally Posted by Anais
nearly done with first semester, wooo

Grats though, that's pretty amazing!
The time flies by, you will be done before you know it!
Programming homework and newbie help thread Quote
12-01-2014 , 04:34 PM
I barely got started.
Programming homework and newbie help thread Quote
12-02-2014 , 02:06 PM
Hey guys I recently had the assignment to find triplets for a^3 + b ^ 3 = c^2 with a given max of c as userinput with a <= b here the first couple triplets:

(1.0, 2.0, 3.0)
(2.0, 2.0, 4.0)
(4.0, 8.0, 24.0)
(8.0, 8.0, 32.0)
(9.0, 18.0, 81.0)
(18.0, 18.0, 108.0)

That obv wasn`t a problem and I quickly came up with this algorithm:

http://pastebin.com/bmN2b98W (It`s actually a different version than I had, I mustve changed it at some point so might not be the prettiest). Anyways for myself I wanted to come up with a more efficent algorithm. I quickly found I don`t exactly know what you call it, "incrementation sequencies".

http://pastebin.com/nx2RL2PF (Now there might be a lot of garbage in there, that I once needed but then changed that I haven`t thrown out). So now the problem I left some stuff out but to make it work I`d have to have

Quote:
cSumme = a*a*a +b*b*b;
c = Math.sqrt(cSumme);
if(c == (long)c && c*c <=maxgrenze*maxgrenze)
after each increment. It makes the code really big and kinda unreadable are there any solution to only like write it once or something?

edit:

One or two sequencies are wrong but that doesn`t matter it was just about the idea and I don`t really care about the results anymore.

edit2:

I`m in my first semester and I`m obv a huge java noob.

Last edited by NiSash1337; 12-02-2014 at 02:20 PM.
Programming homework and newbie help thread Quote
12-03-2014 , 01:41 AM
NiSash1337 why don't you approach the problem in steps.

1. Figure out how to code in java a way to check if three variables; a,b, and c are equal for your equation below.

Example using your first example (1.0, 2.0, 3.0),
in java a^3 + b ^ 3 = c^2, replace them and write code to test if this returns true or false... 1^3 + 2^3 = 3^2 by a method like... public boolean checkEquation(a,b,c) {...}

2. Now that you have a method for returning true or false, write 3 for loops with inputing into the method like...
Quote:
int a = 0; b = 0; c = 0;
for (a; a<=max; a++) {
..if(checkEquation(a,b,c))
....System.out.println("("+a+", "+b+", "+c+")")
..for (b; b<=max; b++) {
....if(checkEquation(a,b,c))
......System.out.println("("+a+", "+b+", "+c+")")
....for (c; c<=max; c++)
......if(checkEquation(a,b,c))
........System.out.println("("+a+", "+b+", "+c+")")
..}
}
you could also just remove the if true checks and output in the method the system.out.println.

I think that is how to make it work and have the code sort of clean but I'm not a math wizard.

Last edited by iosys; 12-03-2014 at 01:49 AM.
Programming homework and newbie help thread Quote

      
m