My following question regards a problem I am having remembering python code. It has been about a year and a half since I last opened the shell or wrote any sort of code. I am attempting to rewrite the bullseye program that was seen in the Greater New York Programming Contest (year 2004 I believe). Here is the link if anyone is interested in re-writting or tweaking some neat programs I am told that the region between each pair of rings (or the center and the first ring) represents a certain point value. The closer the region is to the center of the dartboard, the more points the region is worth (as a normal dartboard is). I have decided to make the ring radii at 3" 6" 9" 12" and 15", while the bullseye is 6" in diameter.
So now is where the fun starts.
I want to create the program so when it runs, the first player throws 3 darts and the score is tallied up. The darts are then removed and the 2nd player repeats the process. At the end I will have it read something such as
SCORE: N to M, Player P win (or state if it is a tie). I am making N player 1's score and M player 2's score. However I am a bit confused on the next part of the instructions which state:
That a text file (games.txt) will contain one or more datasets
"A dataset is a line with 12 float values separated by spaces. Each pair of values represents the X and Y distances respectively of a dart from the center of the board in inches. (the center is located at X = 0, Y = 0. The range of values are:
−20.0 <= X, Y <= 20.0. Player one’s darts are represented by the first 3 pairs of values, and player two’s by the last 3 pairs of values. Input is terminated by the first value of a dataset being -100."
I am not asking for someone to write this up for me because I would like to try it myself, but that last part has me a bit confused. I am fuzzy on what exactly that last part means. If perhaps someone can explain it?
Also I decided to use a function such as score(x,y) which will return the score of a dart thrown at the coordinates(x,y). What I mean by this is something like: score(0,0) would be a bullseye for 100 points while score(-9,0) would hit the 3rd ring for 60 points.
Does that function seem reasonable to use? Thats what Ive come up with so far, I will continue posting my progress as I receive feedback. Any help, suggestions, comments would be greatly appreciated. Thank you for your time.

## Recommended Answers

print score(0,-9)
will trigger the last return in this block of code, because x=0. Note that the "y" portion of the code will never be reached, because any value of x will result in a return from the function.

``````if x>15 or x<-15:
print "Out of Bounds, …``````

Use the modulo "%" which gives the remainder.

``````for x in [1.1, 1.8, 1.0]:
if x % 1 > 0:
y = int(x)+1
print y
else:
print "no conversion for", x
#
# prints
2
2
no conversion for 1.0``````

## All 8 Replies

This is what I have so far. Please can anyone guide me in the right direction? My Y coordinate doesnt seem to be working. When I plug something in for X it works fine, however when I plug in a value for Y, it does not work. It returns 100 regardless of what I put. Does anyone know why this happens, or what I can do to make it smoother? Any help would be great thank you.

``````def main() :

##for example w.e I put in for Y returns 100. While my X value returns what it should.
print score(0,-9)

def score(x,y):

if x>15 or x<-15:
print "Out of Bounds, No Points"
return 0

else:
if x>12 or x<-12:
return 20

else:
if x>9 or x<-9:
return 40

else:
if x >6 or x<-6:
return 60

else:
if x >3 or x<-3:
return 80

else:
return 100

if y>15 or y<-15:
print "Out of Bounds, No Points"
return 0

else:
if y>12 or y<-12:
return 20

else:
if y>9 or y<-9:
return 40

else:
if y>6 or y<-6:
return 60

else:
if y>3 or y<-3:
return 80

else:
return 100

main()``````

print score(0,-9)
will trigger the last return in this block of code, because x=0. Note that the "y" portion of the code will never be reached, because any value of x will result in a return from the function.

``````if x>15 or x<-15:
print "Out of Bounds, No Points"
return 0

else:
if x>12 or x<-12:
return 20

else:
if x>9 or x<-9:
return 40

else:
if x >6 or x<-6:
return 60

else:
if x >3 or x<-3:
return 80

else:
##======================================
##  x=0 will wind up here
##======================================
return 100``````

Also, you could do something a little more succinct like the following.

``````if x>15 or x<-15:
print "Out of Bounds, No Points"
return 0
## else is not necessary since you have a return that will exit the function if > 15
test_list = [ (12, 20), (9, 40), (6, 60), (3, 80)]
for num, ret_val in test_list:
if abs(x) > num:
return ret_val

##  nothing found earlier = abs(x) <= 3
return 100``````

Perfect thank you so much. I figured that out. I have another piece of code which I am having problem figuring out what the error is. It says cannot assign operator when I try to write the formula for a circle.

``````def main():
print score(3,4)
def score(x,y):
r=6
(r**2) = (x**2) + (y**2) ## syntax error here reads "***cannot assign operator"
if r==0:
return 100
else:
r=r/3
p=120-20*r
return p
main()``````

I am also trying to get it to where if it prints out 1.1 it would round to 2.
Again any help would be appreciated.

Use the modulo "%" which gives the remainder.

``````for x in [1.1, 1.8, 1.0]:
if x % 1 > 0:
y = int(x)+1
print y
else:
print "no conversion for", x
#
# prints
2
2
no conversion for 1.0``````

The syntax error remains =/ I had forgotten about the mod for remainders. Thank you. It still says that:
(r**2)=(x**2)+(y**2) is an error.
It reads "cannot assign operator".
Another thing, the guideline says that the Input consists of a text file called games.txt containing one or more datasets.
That just means that at the top of my program I need to run it through games.txt?
Lastly, the does not equal symbol in python is ??? I think its =/ or /= . Am I anywhere close?

Nevermind about the does not equal, just remembered that it is !=.
Now if i could figure out that "cannot assign operator" error and how to run my program through/with games.txt

``(r**2) = (x**2) + (y**2) ## syntax error here reads "***cannot assign operator"``

This is obvious. You can not solve for r squared. You would have to use

``````r2 = (x**2) + (y**2) ## syntax error here reads "***cannot assign operator"
r = r2 **0.5
or r = math.sqrt(r2)``````

Perfect Thank you so much. I got it all taken care of. Now I am gonna try to convert it to C++. Thanks again. Ill post something in the C++ forum if I need any help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.