[Tutor] What's the invalid syntax?
Bob Gailer
bgailer at alum.rpi.edu
Thu Aug 24 02:20:51 CEST 2006
Nathan Pinno wrote:
> What's the invalid syntax in the following code?
Is this a test? Do you want to see how smart we are? Or are you really
trying to find and correct the problems? If the latter please OH please
do as we have requested MANY TIMES - post the traceback.
I'm also concerned, considering the progress you've made in
understanding Python, that this program should stump you. To my eye
there are several syntax errors that stand out like a sore thumb.
Please look at the traceback, notice which line it complains about, then
look at each piece of that line to see whether is is a valid Python element.
Aside from all of that I think this is a pretty poor example of
programming. The use if bitwise operators instead of boolean is not
wrong, but could be misleading or give incorrect results in other
situations. There are no user input checks to reject nicely non-integer
input. There are statements that can be factored out of the loop.
>
> # Hurkle Hunt
> # This program is a game where you have to find a hurkle that the
> computer hides in a grid.
> # Originally in Computer World pg. 70
>
> import random
>
> print "Hunt the Hurkle"
> print
> while 1:
> ans = raw_input("Do you want to play a game? Type Y to play, any
> other letter to quit.")
> while ans == "y" | ans == "Y":
> E = random.choice(range(9))
> N = random.choice(range(9))
> k = 1
> while k <= 5:
> print "Guess ", %d1.0, "."(k)
> X = int(raw_input("East Guess: "))
> Y = int(raw_input("North Guess: "))
> if X > E & Y == N:
> print "Go West"
> k = k + 1
> elif X > E & Y > N:
> print "Go SouthWest"
> k = k + 1
> elif X > E & Y < N:
> print "Go NorthWest"
> k = k + 1
> elif X == E & Y > N:
> print "Go South"
> k = k + 1
> elif X == E & Y < N:
> print "Go North"
> k = k + 1
> elif X < E & Y == N:
> print "Go East"
> k = k + 1
> elif X < E & Y > N:
> print "Go SouthEast"
> k = k + 1
> elif X < E & Y < N:
> print "Go NorthEast"
> k = k + 1
> else:
> print "Congrats! You found the hurkle in", %d1.0, "
> guesses!" (k)
> break
> else:
> print "You didn't find the hurkle in 5 tries! It was at:
> ", E, ",", N
> break
> else:
> break
> print "Goodbye!"
> break
> Thanks!
> Nathan Pinno
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
--
Bob Gailer
510-978-4454
More information about the Tutor
mailing list