[Tutor] beginner's trouble with concepts

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon May 17 03:17:01 EDT 2004



On Thu, 13 May 2004, Geoffrey Bennett wrote:

> The first problem is in the exercise for Booleans:
>
> <http://www.honors.montana.edu/~jjc/easytut/easytut/node12.html>
>
> I cannot get the code to cycle through once per guess. When the user
> makes a guess, if they guess correctly, the script prints out three
> statements of affirmation in a row and then exits instead of one
> confirmation of a correct guess and then exiting. When they guess
> incorrectly, the same thing happens. (The failure message prints all
> three times. So, at least the correct response is being given back to
> the user based upon their input.) However, it should register an
> incorrect guess and then prompt the user for another guess, up to
> three guesses before exiting. Here's the script:

[some text cut]


Hi Geoffrey,


I notice in the code that the user prompt:

> name = raw_input("Guess my name: ")

is outside the body of the while loop.  Anything that's outside the loop
body isn't revisited when the loop restarts, so you probably want to add
the raw_input() line in the while loop body too.



> The second problem is in the modules section:
>
> <http://www.honors.montana.edu/~jjc/easytut/easytut/node14.html>
>
> I am trying to set the value of the number to be guessed to the last two
> digits in the current year. The value does get set correctly, but all
> evaluations see the user's guess as being of a lower value than the
> number set by the script to be guessed. Even guesses of numbers with
> higher values than the number to be guessed are responded to by the
> script as being too low.

Ah, I see it.  The get_num() function:


> # Now use the last two digits from the current time to assign the number
> # to be guessed in a random fashion.
> def get_num():
> 	ct = ctime(time())
> 	yy = ct[-2:]
> 	return yy

actually isn't returning a number: it's returning a string of the two
digits.

    http://www.python.org/doc/lib/module-time.html#l2h-1758

There is a difference between the string "42" and the number 42, and
comparing a string and integer is "undefined" behavior in the sense that I
have no clue what happens.


To fix the problem, we can use the 'int()' converter function to go from
strings back to numeric integers:

###
def get_num():
    ct = ctime(time())
    yy = ct[-2:]
    return int(yy)
###

That should fix the weird bug you're encountering.


If we didn't have to use time.ctime(), I'd recommend using the 'datetime'
module instead:

    http://www.python.org/doc/lib/datetime-date.html

###
>>> import datetime
>>> today = datetime.date.today()
>>> today.year
2004
>>> today.year % 100
4
###

This allows us to avoid mainpulating strings, and is less error prone.
But then, the point in Josh's tutorial there is to learn how to manipulate
strings, so I guess that's not a big issue.  *grin*


If you have more questions, please feel free to ask them.  Good luck!




More information about the Tutor mailing list