[Tutor] Tutor Digest, Vol 85, Issue 91

Lea Parker lea-parker at bigpond.com
Fri Mar 25 07:04:15 CET 2011


Message 3 - I solved my problem. Yah Me!!!

Thanks 

P.S I hope this is the right way to let you know so you don't waste  your
time.

Lea

-----Original Message-----
From: tutor-bounces+lea-parker=bigpond.com at python.org
[mailto:tutor-bounces+lea-parker=bigpond.com at python.org] On Behalf Of
tutor-request at python.org
Sent: Friday, 25 March 2011 4:28 PM
To: tutor at python.org
Subject: Tutor Digest, Vol 85, Issue 91

Send Tutor mailing list submissions to
	tutor at python.org

To subscribe or unsubscribe via the World Wide Web, visit
	http://mail.python.org/mailman/listinfo/tutor
or, via email, send a message with subject or body 'help' to
	tutor-request at python.org

You can reach the person managing the list at
	tutor-owner at python.org

When replying, please edit your Subject line so it is more specific than
"Re: Contents of Tutor digest..."


Today's Topics:

   1. Re: Guessing Game Program (Malcolm Newsome)
   2. Re: Guessing Game Program (Donald Bedsole)
   3. Error in programming (Lea Parker)


----------------------------------------------------------------------

Message: 1
Date: Thu, 24 Mar 2011 23:37:18 -0500
From: "Malcolm Newsome" <malcolm.newsome at gmail.com>
To: "'Donald Bedsole'" <drbedsole at gmail.com>,	<Tutor at python.org>
Subject: Re: [Tutor] Guessing Game Program
Message-ID: <00f401cbeaa6$54049bd0$fc0dd370$@gmail.com>
Content-Type: text/plain;	charset="us-ascii"

Hey Don!

I posted an eerily similar request to another python group about two weeks
ago!  I, too, am very new to programming and the guessing game was my first
shot at writing a script from scratch!

Below is my code (improved with some help from others).  I still would like
to make some improvements to it also.  But, perhaps there will be some ideas
in it that can help you as well!  Looking forward to learning and growing!

All the best!

Malcolm 



# guess.py
# a simple number guessing game

import random

#helper
method----------------------------------------------------------------------
-----------
def nope_message(random_num):
    return "Nope! I'm smarter than you!\nI was thinking of the number: %d" %
int(random_num)

def right_message(retried=False): 
    message = "That was right! I guess you ARE smarter than me"
    
    if retried:
        message += "... even though it took you another try!"

    return message

def reread_input(message):
    return int(input("You were too %s. Type another number: " % message))

def retry(message, random_num):
    guess_iflow = reread_input(message)

    if guess_iflow == random_num:
        return right_message(True)
    else:
        return nope_message(random_num)

#---------------------------------------------------------------------------
------------------

def main():
    print "Do you think you're smarter than me?"
    print "I guess we'll see!"
    print "I'm thinking of a number between 1 - 100.  Can you guess what it
is?"

    random_num = random.randint(1, 100)

    guess = int(input("Type a number between 1 - 100: "))

    error = guess < 1, guess < 100


    if guess == random_num:
        print right_message()
    elif guess < random_num:# user gets second chance if number is too low
        print retry("low", random_num)
    elif guess > random_num:# user gets second chance if number is too high
        print retry("high", random_num)
    else:
        print nope_message(random_num)


if __name__ == "__main__": 
    main()








-----Original Message-----
From: tutor-bounces+malcolm.newsome=gmail.com at python.org
[mailto:tutor-bounces+malcolm.newsome=gmail.com at python.org] On Behalf Of
Donald Bedsole
Sent: Thursday, March 24, 2011 10:55 PM
To: tutor
Subject: [Tutor] Guessing Game Program

Hi folks,

This is a little program I've written to bring together some things I've
been trying to learn (not an attempt, of course, to make an interesting
game)..  I've been working my way through a beginner's tutorial, and except
for a very basic program I wrote in C++ one time, I think this is the first
program I've ever done from scratch.  Of course, I've incorporated ideas
I've learned from folks on the Internet, my tutorial, and this list.

I"m sure I've broken some rules along the way, but it does work without any
errors (that I have found).

So, how could I improve it?  Is it readable to you, or a mess?

Thanks for your time,

Don


#Author D.Bedsole
#drbedsole at gmail.com
#3/24/10
#License: Public Domain


import random
from sys import exit

#A guessing game where user must guess a number between 1 and 10



def start_up():
    print "Hello, and welcome to the Guessing Game. Here are the rules:\n"
    print "Try to guess the correct number.  It is between 1 and 10."
    print "You have ten chances."
    print "To quit the game just type 'quit' (w/o the quotes) at the
prompt.\n"
    print "Here we go!\n"



def ran_num():
    the_number = random.randint(1,10)
    return the_number

def guess_loop(the_number):
        start_number = 0
        while start_number < 10:
            print "Please guess a number between 1-10. Enter your guess at
the prompt."
            guess = (raw_input("> "))	
	    if "exit" in guess: #Give user a chance to exit w/o finishing
game
                print "Thanks for playing. Goodbye!"
	        exit()
	
	
	    if  guess.isdigit(): #validate input
	        guess = int(guess)
	    else:
	        print "You didn't enter a number."
	        continue #Return to top of the loop so user can enter number

	
	
	
	    if guess > the_number:
	        print "Sorry. You guessed too high."
	        start_number += 1
	        attempt_counter(start_number, the_number)#warn user of
remaining tries

	    elif guess < the_number:
	        print "Sorry. That was too low.\n"
                start_number += 1
	        attempt_counter(start_number, the_number)#warn user of
remaining tries
	    else:
	        print "Congratulations. You guessed it!\n"
	        print "Do you want to play again? Type 'y' for yes and 'n'
for no"
	        response = raw_input("> ")
		if 'y' in response:
		    start_up(guess_loop(ran_num()))
	        else:
		    print "Thanks for playing.  Goodbye."	
       		    exit()

            	
#Track guesses attempted and warn of end of game when chances exhausted def
attempt_counter(start_number, the_number):
    print "That was attempt #%d." % (start_number)
    if start_number == 10:
        print "That was your last chance.  The correct answer was %d."
% (the_number)
        print "Do you want to start a new game. Type 'y' for yes and 'n' for
no."
	answer = raw_input("> ")
        if 'y' in answer:
	    start_up(guess_loop(ran_num()))
	else:
	    print "Thanks for playing.  Goodbye."
            exit()



#function calls to start program
start_up()
guess_loop(ran_num())
_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



------------------------------

Message: 2
Date: Fri, 25 Mar 2011 00:55:33 -0400
From: Donald Bedsole <drbedsole at gmail.com>
To: tutor <Tutor at python.org>
Subject: Re: [Tutor] Guessing Game Program
Message-ID:
	<AANLkTi=COEpE5j-8TxSyUU_UyXikJGLEKYPsQO=ga4cJ at mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

On Fri, Mar 25, 2011 at 12:53 AM, Donald Bedsole <drbedsole at gmail.com>
wrote:
> Hi Malcolm :-)
>
> On Fri, Mar 25, 2011 at 12:37 AM, Malcolm Newsome 
> <malcolm.newsome at gmail.com> wrote:
>> Hey Don!
>>
>> I posted an eerily similar request to another python group about two 
>> weeks ago! ?I, too, am very new to programming and the guessing game 
>> was my first shot at writing a script from scratch!
>
> I got interested in writing a guessing game because I was trying to 
> fix a C++ program that wouldn't compile with g++ because they were 
> using a non-standard randomizer() function. ?(I really don't know C++, 
> but I thought trying to fix the problems with someone else's program 
> might help me to learn). ?I didn't make much headway in understanding 
> how to generate random numbers in C++, but it made me curious about 
> how to do it in Python. ?Python seems much easier!
>
>>
>> Below is my code (improved with some help from others). ?I still 
>> would like to make some improvements to it also. ?But, perhaps there 
>> will be some ideas in it that can help you as well! ?Looking forward to
learning and growing!
>>
>> All the best!
>>
>> Malcolm
>
>
> Thanks for posting your code. ?I will look at it later (closing in on
> 1:00 AM here) to see what I can learn from it. ?Tutorials are great, 
> but it seems looking at code makes it easier for me to learn.
>
> Thanks for taking the time to post, and I hope you're successful in 
> your programming studies.
>
> Don
>


------------------------------

Message: 3
Date: Fri, 25 Mar 2011 16:27:41 +1100
From: "Lea Parker" <lea-parker at bigpond.com>
To: <tutor at python.org>
Subject: [Tutor] Error in programming
Message-ID: <000001cbeaad$5ce95b30$16bc1190$@bigpond.com>
Content-Type: text/plain; charset="us-ascii"

Hello

 

Just wondering if you have some time to cast your eyes over another  basic
program.

 

# Prompt user for data

def main():

    print 'This program is to calculate your ticket sales to the softball
game'

    print                                   #blank line

 

    # Value of each level of seat

    a_seat = 15.00

    b_seat = 12.00

    c_seat = 9.00

 

    # Obtain data

    sales_a = int (raw_input('Enter the number of class A tickets sold '))

    sales_b = int (raw_input('Enter the number of class B tickets sold '))

    sales_c = int (raw_input('Enter the number of class C tickets sold ')) 

    income_generated(a_seat, b_seat, c_seat, sales_a, sales_b, sales_c)

 

# Obtain data to determine income generated from sales

def income_generated(a_seat, b_seat, c_seat, sales_a, sales_b, sales_c):

    total_sales = """times the seat value by the number of seats sold for
each seat

    and add totals togeter"""(sale_a * a_seat) + (sale_b * b_seat) + (sale_c
* c_seat)

 

    #Display result to user

    print int ('Your total sales for the softball game are: $ ',
total_sales)

 

# Call the main function

main()

 

I get the following errors:

>>> ================================ RESTART
================================

>>> 

This program is to calculate your ticket sales to the softball game

 

Enter the number of class A tickets sold 5

Enter the number of class B tickets sold 5

Enter the number of class C tickets sold 10

 

Traceback (most recent call last):

  File "F:/Backups/MY Documents26.2.11/Documents/Lea University/CSU/ITC10 -
Programming Principles/2011/Assessment Tasks/Assessment 1b and
1c/Stadium_Seating.py", line 29, in <module>

    main()

  File "F:/Backups/MY Documents26.2.11/Documents/Lea University/CSU/ITC10 -
Programming Principles/2011/Assessment Tasks/Assessment 1b and
1c/Stadium_Seating.py", line 18, in main

    income_generated(a_seat, b_seat, c_seat, sales_a, sales_b, sales_c)

  File "F:/Backups/MY Documents26.2.11/Documents/Lea University/CSU/ITC10 -
Programming Principles/2011/Assessment Tasks/Assessment 1b and
1c/Stadium_Seating.py", line 23, in income_generated

    and add totals togeter"""(sale_a * a_seat) + (sale_b * b_seat) + (sale_c
* c_seat)

NameError: global name 'sale_a' is not defined

>>> 

 

My way of thinking is firstly I need to fix line 29 which is main(), I tried
to do this by adding the brackets around text output in line 26. This seemed
to allow me to type main against margin rather than it wanting to indent but
didn't fix the problem. Your suggestions would be appreciated.

 

Thank

Lea

-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.python.org/pipermail/tutor/attachments/20110325/2a3d1c33/attach
ment.html>

------------------------------

_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor


End of Tutor Digest, Vol 85, Issue 91
*************************************



More information about the Tutor mailing list