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 -
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
14 3693 - 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
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) -
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!
how about how to draw the turtle function
determine by the value of P1 and P2?
What exactly are you trying to draw? Surely you're just trying to draw something to do with the nugget values you get?
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
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:
2. then you use this value in your gold function:
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.
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: - 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
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
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: - 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: - 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
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
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.
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...? -
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)
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| | |