[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. 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.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </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> </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> sum =
0<BR> t = 0<BR> for n in
in_string:<BR> t =
int(n)<BR> sum = sum +
t<BR> return sum</DIV>
<DIV> </DIV>
<DIV>print """This program finds all the integers in a user-specified
range<BR>that have a user-specified digit total."""</DIV>
<DIV> </DIV>
<DIV>max_possible = 0<BR>run_again = "y"</DIV>
<DIV> </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":
<BR> min_number = raw_input("Enter a start integer:
")<BR> max_number = raw_input("Enter a stop integer:
")<BR> digit_total = raw_input("What digit total would you
like: ")<BR> <BR> x =
""<BR> p = 0<BR> running_total = 0</DIV>
<DIV> </DIV>
<DIV>#Passes each number in the range to sum_of_digits function as a
string.<BR> for y in range (int(min_number),
int(max_number)+1):<BR> x =
str(y)<BR> p = sum_of_digits(x)</DIV>
<DIV> </DIV>
<DIV>#Checks to see if condition is met; if so, counter is
incremented.<BR> if p ==
int(digit_total):<BR>
running_total = running_total + 1</DIV>
<DIV> </DIV>
<DIV>#Checks to make sure the user digit total wasn't too big.
<BR> if p >
max_possible:<BR>
max_possible = p<BR> if
int(digit_total)>max_possible:<BR>
print "No possible way to get that digit total in that number
range."<BR> print "The highest total
possible is: ",max_possible<BR>
run_again = raw_input("Would you like to run this program again? y/n:
")<BR> if run_again ==
"y":<BR>
pass</DIV>
<DIV> </DIV>
<DIV>#Shows the number of integers that meet the criteria and allows user to
run<BR>#program again if
desired.
<BR> else:<BR> print
"Total number of integers with a digit total of",digit_total,"is:
",running_total<BR> run_again =
raw_input("Would you like to run this program again? y/n: ")<BR></DIV>
<DIV> </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. 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.</DIV></FONT></BODY></HTML>
--Boundary_(ID_98i9W9TVP61EExAtuef3Fg)--