473,406 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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
Expand|Select|Wrap|Line Numbers
  1. x = 0
  2. y = 0
  3.  
  4. import turtle
  5. import random
  6.  
  7. def gold(side_length):
  8.    turtle.forward(side_length)
  9.    turtle.left(90)
  10.    turtle.forward(side_length)
  11.    turtle.left(90)
  12.    turtle.forward(side_length)
  13.    turtle.left(90)
  14.    turtle.forward(side_length)
  15.  
  16. def nugget():
  17.     import random
  18.     print random.randint(10,100)
  19.  
  20. def turn():
  21.     if
Jan 27 '10 #1
14 3693
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. def nugget():
  4.     return random.randint(10,100)
  5.  
  6. def turn():
  7.     p1 = 0
  8.     p2 = 0
  9.     if p1 == 0:
  10.         print "p1 = ",p1 + nugget()
  11.     else:
  12.         print "p2 = ",p2 + nugget()
what i have got so far
Jan 27 '10 #2
Glenton
391 Expert 256MB
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)

Expand|Select|Wrap|Line Numbers
  1. def turns(n):
  2.     "distributes n nuggets between prospectors"
  3.     for i in range(n):
  4.         if i%2==0:
  5.             # i is even, so it's prospector one's turn
  6.             print "prospector 1"
  7.             prospector=1
  8.         else:
  9.             # i is odd, so it's prospector two's turn
  10.             print "prospector 2"
  11.             prospector=2
  12.         # At this point we have a variable "prospector" which alternates
  13.         # between 1 and 2
  14.         # So now you need your functions
  15.         no_of_nuggets = your_random_nugget_function_here()
  16.         your_movement_function(no_of_nuggets,prospector)
  17.  
Good luck!
Jan 27 '10 #3
how about how to draw the turtle function
determine by the value of P1 and P2?
Jan 27 '10 #4
Glenton
391 Expert 256MB
What exactly are you trying to draw? Surely you're just trying to draw something to do with the nugget values you get?
Jan 27 '10 #5
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
Jan 27 '10 #6
Glenton
391 Expert 256MB
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:
Expand|Select|Wrap|Line Numbers
  1. no_nugs=nugget()
  2.  
2. then you use this value in your gold function:
Expand|Select|Wrap|Line Numbers
  1. gold(no_nugs)
  2.  
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.
Jan 27 '10 #7
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

http://sleepy.cs.surrey.sfu.ca/cmpt/...120nuggets.bmp
Jan 27 '10 #8
Glenton
391 Expert 256MB
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:
Expand|Select|Wrap|Line Numbers
  1. pos=(0,0)
  2. for i in range(n):
  3.     size=randomint(10,100)
  4.     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
Jan 27 '10 #9
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
Jan 28 '10 #10
Glenton
391 Expert 256MB
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:
Expand|Select|Wrap|Line Numbers
  1. def gold(side,posx,posy):
  2.     turtle.position(posx,posy)
  3.     for i in range(4):    
  4.         turtle.forward(side)
  5.         turtle.left(90)
  6.     turtle.reset
  7.     return posx,posy+side
Then your calling code (in turns I guess) is:
Expand|Select|Wrap|Line Numbers
  1. posx=0
  2. posy=0
  3. for i in range(n):
  4.     size=randomint(10,100)
  5.     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
Jan 28 '10 #11
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.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,50)
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(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
Jan 28 '10 #12
Glenton
391 Expert 256MB
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.
Jan 28 '10 #13
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...?
Expand|Select|Wrap|Line Numbers
  1. import turtle, random, math
  2.  
  3. ###def jump_to(x,y):
  4.     #turtle.up()
  5.     #turtle.goto(x,y)
  6.     #turtle.down()
  7.  
  8. r = random.randint(0,1)
  9. g = random.randint(0,1)
  10. b = random.randint(0,1)
  11.  
  12. def gold_1(result1):
  13.     turtle.up()
  14.     turtle.goto(0,result1)
  15.     turtle.down()
  16.     turtle.setheading(0)
  17.     turtle.fill(1)
  18.     turtle.color(r,g,b)
  19.     turtle.setheading(0)
  20.     turtle.forward(result1)
  21.     turtle.left(90)
  22.     turtle.forward(result1)
  23.     turtle.left(90)
  24.     turtle.forward(result1)
  25.     turtle.left(90)
  26.     turtle.forward(result1)
  27.     turtle.fill(0)
  28.     turtle.setheading(0)
  29.  
  30.  
  31. def gold_2(result2):
  32.     turtle.up()
  33.     turtle.goto(100,result2)
  34.     turtle.down()
  35.     turtle.setheading(0)
  36.     turtle.fill(1)
  37.     turtle.color(r,g,b)
  38.     turtle.forward(result2)
  39.     turtle.left(90)
  40.     turtle.forward(result2)
  41.     turtle.left(90)
  42.     turtle.forward(result2)
  43.     turtle.left(90)
  44.     turtle.forward(result2)
  45.     turtle.fill(0)
  46.     turtle.setheading(0)
  47.  
  48. def nugget():
  49.     return random.randint(10,100)
  50.  
  51. def turn(n):
  52.     p1 = 0
  53.     p2 = 0
  54.     posx = 0
  55.     posy = 0
  56.  
  57.     for x in range(1, n):
  58.         p1 += nugget()
  59.         p2 += nugget()
  60.  
  61.         print 'p1 = ' ,str (p1)
  62.         print 'p2 = ' ,str (p2)
  63.  
  64.     if n%2 ==0:
  65.         result1 = p1 + nugget()
  66.         turtle.up()
  67.         turtle.goto(0,result1)
  68.         turtle.down()
  69.         turtle.setheading(0)
  70.         turtle.fill(1)
  71.         turtle.color(r,g,b)
  72.         turtle.setheading(0)
  73.         turtle.forward(result1)
  74.         turtle.left(90)
  75.         turtle.forward(result1)
  76.         turtle.left(90)
  77.         turtle.forward(result1)
  78.         turtle.left(90)
  79.         turtle.forward(result1)
  80.         turtle.fill(0)
  81.         turtle.setheading(0)
  82.  
  83.     else:
  84.         n%1==0
  85.         result2 = p2 + nugget()
  86.         turtle.up()
  87.         turtle.goto(100,result2)
  88.         turtle.down()
  89.         turtle.setheading(0)
  90.         turtle.fill(1)
  91.         turtle.color(r,g,b)
  92.         turtle.forward(result2)
  93.         turtle.left(90)
  94.         turtle.forward(result2)
  95.         turtle.left(90)
  96.         turtle.forward(result2)
  97.         turtle.left(90)
  98.         turtle.forward(result2)
  99.         turtle.fill(0)
  100.         turtle.setheading(0)
Jan 28 '10 #14
bvdet
2,851 Expert Mod 2GB
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
Jan 29 '10 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
34
by: Erik Johnson | last post by:
This is somewhat a NEWBIE question... My company maintains a small RDBS driven website. We currently generate HTML using PHP. I've hacked a bit in Python, and generally think it is a rather...
2
by: Hal Vaughan | last post by:
I'm self taught and most of what I've been working on for the past several years has been entirely in Perl and Java. I've noticed that I can code about 5 times faster in Perl than Java, in part...
122
by: seberino | last post by:
I'm interested in knowing which Python web framework is most like Ruby on Rails. I've heard of Subway and Django. Are there other Rails clones in Python land I don't know about? Which one...
35
by: John Coleman | last post by:
Greetings, I have a rough classification of languages into 2 classes: Zen languages and tool languages. A tool language is a language that is, well, a *tool* for programming a computer. C is the...
267
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at...
158
by: Giovanni Bajo | last post by:
Hello, I just read this mail by Brett Cannon: http://mail.python.org/pipermail/python-dev/2006-October/069139.html where the "PSF infrastracture committee", after weeks of evaluation, recommends...
852
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no axes to grind here. This is just a question for...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.