Needed help on python, easy (about taking turns)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wayway409
    New Member
    • Jan 2010
    • 8

    Needed help on python, easy (about taking turns)

    In this sharing algorithm the prospectors take it in turns to get gold nuggets. Write a function called turns(n) to calculate the amount of gold that each prospector gets, and to display this in Turtle Graphics. Your function should look something like this pseudocode version. The function should have a single parameter, the number of gold nuggets to be shared between the prospectors. I expect that you will want to write other functions that are called by turns (to move the turtle, draw squares and so on).

    set p1's gold to 0
    set p2's gold to 0
    for a given number of nuggets
    set nugget to a random number between 10 and 100 (its value)
    if it is prospetor1's turn
    add nugget to p1
    display prospector1's nugget
    else
    add nugget to p2
    display prospector2's nugget



    Here is what i have got, but i do not know how to take turns and show the result of each player
    Code:
    x = 0
    y = 0
    
    import turtle
    import random
    
    def gold(side_length):
       turtle.forward(side_length)
       turtle.left(90)
       turtle.forward(side_length)
       turtle.left(90)
       turtle.forward(side_length)
       turtle.left(90)
       turtle.forward(side_length)
    
    def nugget():
        import random
        print random.randint(10,100)
    
    def turn():
        if
    Last edited by bvdet; Jan 27 '10, 01:35 PM. Reason: Add code tags
  • wayway409
    New Member
    • Jan 2010
    • 8

    #2
    Code:
    import random
    
    def nugget():
        return random.randint(10,100)
    
    def turn():
        p1 = 0
        p2 = 0
        if p1 == 0:
            print "p1 = ",p1 + nugget()
        else:
            print "p2 = ",p2 + nugget()
    what i have got so far
    Last edited by bvdet; Jan 27 '10, 01:35 PM. Reason: Add code tags

    Comment

    • Glenton
      Recognized Expert Contributor
      • Nov 2008
      • 391

      #3
      I don't quite understand. But I think you're saying that you add a random number to p1 and p2 in turn, and also extend a line from p1 and p2 in turn, so you can see them race against each other in a way.

      As with all things there are many ways of doing this. But since this is homework and your main problem seems to be with taking the turns, I'll help with that aspect. Basically we need to go through n nuggets, splitting them between the two prospectors. I'll split the code using the % operator, which essentially returns the remainder (e.g. 10%3 is equal to 1)

      Code:
      def turns(n):
          "distributes n nuggets between prospectors"
          for i in range(n):
              if i%2==0:
                  # i is even, so it's prospector one's turn
                  print "prospector 1"
                  prospector=1
              else:
                  # i is odd, so it's prospector two's turn
                  print "prospector 2"
                  prospector=2
              # At this point we have a variable "prospector" which alternates
              # between 1 and 2
              # So now you need your functions
              no_of_nuggets = your_random_nugget_function_here()
              your_movement_function(no_of_nuggets,prospector)
      Good luck!

      Comment

      • wayway409
        New Member
        • Jan 2010
        • 8

        #4
        how about how to draw the turtle function
        determine by the value of P1 and P2?

        Comment

        • Glenton
          Recognized Expert Contributor
          • Nov 2008
          • 391

          #5
          What exactly are you trying to draw? Surely you're just trying to draw something to do with the nugget values you get?

          Comment

          • wayway409
            New Member
            • Jan 2010
            • 8

            #6
            yup, but i do not know how to draw turtle value according to the random nugget value i get, the result should be square stalking one by one.

            and compare side by side.

            i would be very happy if you can help me
            thats the only part i am stuck at

            Comment

            • Glenton
              Recognized Expert Contributor
              • Nov 2008
              • 391

              #7
              I'm afraid your explanation has not helped me. Could you be clearer?

              However:
              1. you set a variable equal to the value from your nugget function:
              Code:
              no_nugs=nugget()
              2. then you use this value in your gold function:
              Code:
              gold(no_nugs)
              If you want two different squares, then you change your gold function so that it can also take the prospector variable (1 or 2), and use that variable to change where you draw the square.

              Can you also please use code tags - it makes it much clearer.

              Comment

              • wayway409
                New Member
                • Jan 2010
                • 8

                #8
                actually, when i use turn(1) it give me p1 and p2 value
                and turn(2) it gives me 2nd p1 and p2 value..

                so it kinda applies to building up squares, stacking up like this

                Comment

                • Glenton
                  Recognized Expert Contributor
                  • Nov 2008
                  • 391

                  #9
                  It's been a while since I used turtle. It can be a little tricky.
                  What I would do is use something broadly like this:
                  1. Make your gold function take a side length and a starting position.
                  2. Lift the pen and go to the starting position
                  3. Draw the square
                  4. Calculate and return the value of the next position.

                  Then the calling code (turn, I guess, you haven't shown us your code yet) would initialise with starting positions for each of the prospectors and then update the starting position with each call to turn. Something like:
                  Code:
                  pos=(0,0)
                  for i in range(n):
                      size=randomint(10,100)
                      pos=gold(size,pos)
                  This isn't actual code, it's just meant to give you an idea of how to structure it. If you're still struggling, please be prepared to show us the code you've written and exactly what the problem is.

                  Good luck

                  Comment

                  • wayway409
                    New Member
                    • Jan 2010
                    • 8

                    #10
                    import turtle, random, math

                    def gold_1(side):
                    turtle.forward( side)
                    turtle.left(90)
                    turtle.forward( side)
                    turtle.left(90)
                    turtle.forward( side)
                    turtle.left(90)
                    turtle.forward( side)
                    turtle.reset

                    def gold_2(side):
                    turtle.position (50,0)
                    turtle.forward( side)
                    turtle.left(90)
                    turtle.forward( side)
                    turtle.left(90)
                    turtle.forward( side)
                    turtle.left(90)
                    turtle.forward( side)
                    turtle.reset

                    def nugget():
                    return random.randint( 10,100)


                    def turn(x):
                    p1 = 0
                    p2 = 0
                    for i in range(1, x+1):
                    p1 += nugget()
                    p2 += nugget()
                    print 'p1:' + str(p1)
                    print 'p2:' +str(p2)

                    for i in range(1, x+1):
                    print gold_1(p1)
                    print gold_2(p2)







                    This is the output i have done, however, i still cannot get the gold to stack up when its 2nd turn like the image above

                    Comment

                    • Glenton
                      Recognized Expert Contributor
                      • Nov 2008
                      • 391

                      #11
                      Please used code tags. It's much easier to read.

                      I'll explain my previous post in more detail:
                      Make your gold function as follows:
                      Code:
                      def gold(side,posx,posy):
                          turtle.position(posx,posy)
                          for i in range(4):    
                              turtle.forward(side)
                              turtle.left(90)
                          turtle.reset
                          return posx,posy+side
                      Then your calling code (in turns I guess) is:
                      Code:
                      posx=0
                      posy=0
                      for i in range(n):
                          size=randomint(10,100)
                          posx,posy=gold(size,posx,posy)
                      I haven't tried this, but hopefully it gives you the idea (eg I'm not sure how the x and y axes are relative to the screen).

                      The other neat part of this is that you can use the same code for both your prospectors. Just make a posx1, posy1, posx2, posy2, but otherwise a similar formula.

                      All the best

                      Comment

                      • wayway409
                        New Member
                        • Jan 2010
                        • 8

                        #12
                        import turtle, random, math

                        def jump_to(x,y):
                        turtle.up()
                        turtle.goto(50, 0)
                        turtle.down()

                        r = random.randint( 0,1)
                        g = random.randint( 0,1)
                        b = random.randint( 0,1)

                        def gold_1(result1) :
                        turtle.up()
                        turtle.goto(-100,50)
                        turtle.down()
                        turtle.fill(1)
                        turtle.color(r, g,b)
                        turtle.setheadi ng(0)
                        turtle.forward( result1)
                        turtle.left(90)
                        turtle.forward( result1)
                        turtle.left(90)
                        turtle.forward( result1)
                        turtle.left(90)
                        turtle.forward( result1)
                        turtle.fill(0)
                        turtle.setheadi ng(0)


                        def gold_2(result2) :
                        turtle.up()
                        turtle.goto(100 ,50)
                        turtle.down()
                        turtle.setheadi ng(0)
                        turtle.fill(1)
                        turtle.color(r, g,b)
                        turtle.forward( result2)
                        turtle.left(90)
                        turtle.forward( result2)
                        turtle.left(90)
                        turtle.forward( result2)
                        turtle.left(90)
                        turtle.forward( result2)
                        turtle.fill(0)
                        turtle.setheadi ng(0)

                        def nugget():
                        return random.randint( 10,100)

                        def turn(x):
                        p1 = 0
                        p2 = 0
                        for i in range(1, x+1):
                        p1 += nugget()
                        p2 += nugget()
                        print 'p1 = ' ,str (p1)
                        print 'p2 = ' ,str (p2)

                        #if x%2 ==0:
                        result1 = p1
                        print gold_1(result1)
                        #else:
                        #x%1 ==0
                        result2 = p2
                        print gold_2(result2)



                        here is what i have got so far,

                        the only problem i have with this is, i dunno how come it keep overlap.

                        it suppose to stack up

                        Comment

                        • Glenton
                          Recognized Expert Contributor
                          • Nov 2008
                          • 391

                          #13
                          Read previous post.

                          Also try to think about the problem in detail. Perhaps draw it out with pen and paper. Try to describe what to do and then translate it into an algorithm.

                          Comment

                          • wayway409
                            New Member
                            • Jan 2010
                            • 8

                            #14
                            This is my final version and i still encountering the same problem about stacking them up, i know what you are talking about the POS, but when i put it in python, it doesnt seems to work out...do you mind to teach me more detail please...?
                            Code:
                            import turtle, random, math
                            
                            ###def jump_to(x,y):
                                #turtle.up()
                                #turtle.goto(x,y)
                                #turtle.down()
                            
                            r = random.randint(0,1)
                            g = random.randint(0,1)
                            b = random.randint(0,1)
                            
                            def gold_1(result1):
                                turtle.up()
                                turtle.goto(0,result1)
                                turtle.down()
                                turtle.setheading(0)
                                turtle.fill(1)
                                turtle.color(r,g,b)
                                turtle.setheading(0)
                                turtle.forward(result1)
                                turtle.left(90)
                                turtle.forward(result1)
                                turtle.left(90)
                                turtle.forward(result1)
                                turtle.left(90)
                                turtle.forward(result1)
                                turtle.fill(0)
                                turtle.setheading(0)
                                
                            
                            def gold_2(result2):
                                turtle.up()
                                turtle.goto(100,result2)
                                turtle.down()
                                turtle.setheading(0)
                                turtle.fill(1)
                                turtle.color(r,g,b)
                                turtle.forward(result2)
                                turtle.left(90)
                                turtle.forward(result2)
                                turtle.left(90)
                                turtle.forward(result2)
                                turtle.left(90)
                                turtle.forward(result2)
                                turtle.fill(0)
                                turtle.setheading(0)
                            
                            def nugget():
                                return random.randint(10,100)
                            
                            def turn(n):
                                p1 = 0
                                p2 = 0
                                posx = 0
                                posy = 0
                                
                                for x in range(1, n):
                                    p1 += nugget()
                                    p2 += nugget()
                            
                                    print 'p1 = ' ,str (p1)
                                    print 'p2 = ' ,str (p2)
                                    
                                if n%2 ==0:
                                    result1 = p1 + nugget()
                                    turtle.up()
                                    turtle.goto(0,result1)
                                    turtle.down()
                                    turtle.setheading(0)
                                    turtle.fill(1)
                                    turtle.color(r,g,b)
                                    turtle.setheading(0)
                                    turtle.forward(result1)
                                    turtle.left(90)
                                    turtle.forward(result1)
                                    turtle.left(90)
                                    turtle.forward(result1)
                                    turtle.left(90)
                                    turtle.forward(result1)
                                    turtle.fill(0)
                                    turtle.setheading(0)
                            
                                else:
                                    n%1==0
                                    result2 = p2 + nugget()
                                    turtle.up()
                                    turtle.goto(100,result2)
                                    turtle.down()
                                    turtle.setheading(0)
                                    turtle.fill(1)
                                    turtle.color(r,g,b)
                                    turtle.forward(result2)
                                    turtle.left(90)
                                    turtle.forward(result2)
                                    turtle.left(90)
                                    turtle.forward(result2)
                                    turtle.left(90)
                                    turtle.forward(result2)
                                    turtle.fill(0)
                                    turtle.setheading(0)
                            Last edited by bvdet; Jan 29 '10, 12:50 AM. Reason: Add code tags

                            Comment

                            • bvdet
                              Recognized Expert Specialist
                              • Oct 2006
                              • 2851

                              #15
                              wayway409,

                              Please use code tags when posting code. Code is difficult if not impossible to follow without them. Instructions can be found here.

                              BV - Moderator

                              Comment

                              Working...