[Tutor] beginner's trouble with concepts
Geoffrey Bennett
geoffrey at austin.ticom.com
Thu May 13 16:25:54 EDT 2004
Okay, I've never really programmed before (Yeah, I can do "hello world"
in quite a few languages, but who can't?) aside from shell scripting and
basic sed and awk stuff. So, I decided I would learn Python. I've been
using the "Non-Programmers Tutorial for Python" by Josh Cogliati and
been very happy with it. He does a wonderful job of explaining things
and give excellent real world examples to illustrate the concepts. That
having been said, I am tripping over a couple of things. 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:
##############################################################################
#! /bin/env python
## This script has a user guess my name.
## They are only given three tries before the script bails out.
name = raw_input("Guess my name: ")
def check_name(guess):
if guess == "Silly":
print "Correct."
else:
print "Wrong. Try again."
## There is a problem in the next section.
## The system doesn't return to ask for a new guess after a wrong one.
## It just prints out the response for a bad guess three times and exits.
## If you give it a correct guess it prints the response for a correct
## guess three times in a row and exits as well.
count = 0
while count < 3:
check_name(name)
count = count + 1
##############################################################################
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. Now, here's the script:
##############################################################################
#! /bin/env python
from time import *
# 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
number = get_num()
guess = 0
# This next section evaluates the guess incorrectly. When we print out
# the value of "number" to the screen and then choose that *exact* number,
# the response we are given is that the guessed number is too low. In fact,
# all guesses, regardless of the value guessed, get this response - even for
# values above the number to be guessed. Something is really wrong!!!
while guess != number :
guess = input ("Guess a number: ")
if guess > number :
print "Too high"
elif guess < number :
print "Too low"
print "Just right"
##############################################################################
Any ideas as to what I have missed? BTW, I am using Python v 2.3.3 on a
Gentoo Linux system. (Python was compiled by gcc v3.2.3 if anyone
cares.) Thanks for any and all help.
geoffrey
--
++++++++++++++++++++++++++
This space intentionally
left non-blank
++++++++++++++++++++++++++
More information about the Tutor
mailing list