[Tutor] newbie looking for code suggestions

Bob Roher shiska@swbell.net
Sun, 22 Sep 2002 00:12:36 -0500


This is a multi-part message in MIME format.

--Boundary_(ID_98i9W9TVP61EExAtuef3Fg)
Content-type: text/plain; charset=iso-8859-1
Content-transfer-encoding: 7BIT

Hi all.  I was wondering if anyone could look over the following code and give me any suggestions or criticisms?  I just started learning Python a few days ago, so I'm sure there's a lot that can be done to make it look and run better.

#This program was written in response to following question:
#How many numbers between 1 and 999999 have 21 as the total of
#their digits?
#For example: 902,550 is 9+0+2+5+5+0 = 21
#After finding the answer, I wanted to expand it to find any total
#in an integer range.


#This function will take an integer as input and return the sum of
#its digits.
def sum_of_digits(in_string):
    sum = 0
    t = 0
    for n in in_string:
        t = int(n)
        sum = sum + t
    return sum

print """This program finds all the integers in a user-specified range
that have a user-specified digit total."""

max_possible = 0
run_again = "y"

#User enters parameters for min, max, and what I call digit total for
#lack of a better word.
while run_again == "y":  
    min_number = raw_input("Enter a start integer: ")
    max_number = raw_input("Enter a stop integer: ")
    digit_total = raw_input("What digit total would you like: ")
    
    x = ""
    p = 0
    running_total = 0

#Passes each number in the range to sum_of_digits function as a string.
    for y in range (int(min_number), int(max_number)+1):
        x = str(y)
        p = sum_of_digits(x)

#Checks to see if condition is met; if so, counter is incremented.
        if p == int(digit_total):
            running_total = running_total + 1

#Checks to make sure the user digit total wasn't too big.  
        if p > max_possible:
            max_possible = p
    if int(digit_total)>max_possible:
        print "No possible way to get that digit total in that number range."
        print "The highest total possible is: ",max_possible
        run_again = raw_input("Would you like to run this program again?  y/n: ")
        if run_again == "y":
            pass

#Shows the number of integers that meet the criteria and allows user to run
#program again if desired.            
    else:
        print "Total number of integers with a digit total of",digit_total,"is: ",running_total
        run_again = raw_input("Would you like to run this program again?  y/n: ")


One thing I would like to do is be able to have the user re-enter digit_total if they enter one that is too big without re-running the program.  I tried a few things, but am having trouble passing control back to the main while loop if I add another prompt in there.  I would also like to compute max_possible independently so that I could run all possible digit_totals for a range, but I think it would have to run through the entire range to compute that first.  Sorry if I am confusing you, I know what I want it to do, but I'm finding it hard to describe.

--Boundary_(ID_98i9W9TVP61EExAtuef3Fg)
Content-type: text/html; charset=iso-8859-1
Content-transfer-encoding: 7BIT

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2600.0" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>Hi all.&nbsp; I was wondering if anyone could look 
over the following code and give me any suggestions or criticisms?&nbsp; I just 
started learning Python a few days ago, so I'm sure there's a lot that can be 
done to make it look and run better.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>#This program was written in response to following 
question:<BR>#How many numbers between 1 and 999999 have 21 as the total 
of<BR>#their digits?<BR>#For example: 902,550 is 9+0+2+5+5+0 = 21<BR>#After 
finding the answer, I wanted to expand it to find any total<BR>#in an integer 
range.</FONT></DIV>
<DIV>&nbsp;</DIV><FONT face=Arial size=2>
<DIV><BR>#This function will take an integer as input and return the sum 
of<BR>#its digits.<BR>def sum_of_digits(in_string):<BR>&nbsp;&nbsp;&nbsp; sum = 
0<BR>&nbsp;&nbsp;&nbsp; t = 0<BR>&nbsp;&nbsp;&nbsp; for n in 
in_string:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t = 
int(n)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sum = sum + 
t<BR>&nbsp;&nbsp;&nbsp; return sum</DIV>
<DIV>&nbsp;</DIV>
<DIV>print """This program finds all the integers in a user-specified 
range<BR>that have a user-specified digit total."""</DIV>
<DIV>&nbsp;</DIV>
<DIV>max_possible = 0<BR>run_again = "y"</DIV>
<DIV>&nbsp;</DIV>
<DIV>#User enters parameters for min, max, and what I call digit total 
for<BR>#lack of a better word.<BR>while run_again == "y":&nbsp; 
<BR>&nbsp;&nbsp;&nbsp; min_number = raw_input("Enter a start integer: 
")<BR>&nbsp;&nbsp;&nbsp; max_number = raw_input("Enter a stop integer: 
")<BR>&nbsp;&nbsp;&nbsp; digit_total = raw_input("What digit total would you 
like: ")<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; x = 
""<BR>&nbsp;&nbsp;&nbsp; p = 0<BR>&nbsp;&nbsp;&nbsp; running_total = 0</DIV>
<DIV>&nbsp;</DIV>
<DIV>#Passes each number in the range to sum_of_digits function as a 
string.<BR>&nbsp;&nbsp;&nbsp; for y in range (int(min_number), 
int(max_number)+1):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x = 
str(y)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p = sum_of_digits(x)</DIV>
<DIV>&nbsp;</DIV>
<DIV>#Checks to see if condition is met; if so, counter is 
incremented.<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if p == 
int(digit_total):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
running_total = running_total + 1</DIV>
<DIV>&nbsp;</DIV>
<DIV>#Checks to make sure the user digit total wasn't too big.&nbsp; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if p &gt; 
max_possible:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
max_possible = p<BR>&nbsp;&nbsp;&nbsp; if 
int(digit_total)&gt;max_possible:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
print "No possible way to get that digit total in that number 
range."<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "The highest total 
possible is: ",max_possible<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
run_again = raw_input("Would you like to run this program again?&nbsp; y/n: 
")<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if run_again == 
"y":<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
pass</DIV>
<DIV>&nbsp;</DIV>
<DIV>#Shows the number of integers that meet the criteria and allows user to 
run<BR>#program again if 
desired.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<BR>&nbsp;&nbsp;&nbsp; else:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print 
"Total number of integers with a digit total of",digit_total,"is: 
",running_total<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; run_again = 
raw_input("Would you like to run this program again?&nbsp; y/n: ")<BR></DIV>
<DIV>&nbsp;</DIV>
<DIV>One thing I would like to do is be able to have the user re-enter 
digit_total if they enter one that is too big without re-running the 
program.&nbsp; I tried a few things, but am having trouble passing control back 
to the main while loop if I add another prompt in there.&nbsp; I would also like 
to compute max_possible independently so that I could run all possible 
digit_totals for a range, but I think it would have to run through the entire 
range to compute that first.&nbsp; Sorry if I am confusing you, I know what I 
want it to do, but I'm finding it hard to describe.</DIV></FONT></BODY></HTML>

--Boundary_(ID_98i9W9TVP61EExAtuef3Fg)--