[Tutor] (no subject)

Hugo Arts hugo.yoshi at gmail.com
Mon Dec 12 11:56:36 CET 2011


On Mon, Dec 12, 2011 at 3:56 AM, shawn taylor <shtlor at yahoo.com> wrote:
> firstname = raw_input ("Enter your first name ")
> lastname = raw_input ("Enter your last name ")
> idnumber = raw_input ("Enter your id number ")
> birthday = raw_input ("Enter your birthday mm/dd/yyyy ")
> username1 = firstname[0] + lastname[0] + idnumber
>
> print ("Your username is ") + username1
>
> midltr = firstname[len(firstname) / 2]
> midltrlastnm = lastname[len(lastname)  / 2]
>
> on = int (birthday[0])
> tw = int (birthday[1])
> th = int (birthday[3])
> fr = int (birthday[4])
> fv = int (birthday[6])
> sx = int (birthday[7])
> sv = int (birthday[8])
> eg = int (birthday[9])
>
> num = str (on + tw + th + fr + fv + sx + sv + et)
>
> while num > 10:
>
>
> print num
>
> I'm trying to add all the digits in the birthday and keep adding till i get
> a single digit and I'm stuck
> anything would help thanks

alright, well, you've got all the basic ideas that you need in there,
it's just a matter of combining them in the right way. The basic
component is the while loop you have, you want to keep adding the
digits while there's more than two of them:

while num > 10:
    #add digits

there is a slight error in there (what happens if the number comes out
to exactly ten? is that what should happen?), but I'll leave it in for
you to find. Another problem is that the while loop wants to check the
num variable, but it doesn't actually exist until the bottom of the
loop. What we do have at the top of the loop is the birthday variable,
but that has some pesky '/' characters in there that we don't really
want. I suggest the first thing you do is remove those slashes. (hint:
the str class has a replace() method. Google it and see if you can
figure out how to use it for this purpose).

no_slashes_birthday = # use replace here in someway on the birthday?
num = int(no_slashes_birthday)
while num > 10:
    #add digits

that's enough for basic setup of the while loop. Now, to add the
digits. First thing we'll need to do is separate our number out into
digits. There's a nifty trick with the division and modulo operators
that can get us individual digits[1], but I don't want to convolute
the essence of this example with math. So here's a simpler way: leave
your number as a string! As you've already figured out, it's easy to
get characters in a string, and you can convert them to integers
individually again.

Ok, so we don't do num = int(no_slashes_birthday), but just num =
no_slashes_birthday. Now we can get at the digits. All we need to do
now is convert them all into integers and add them together inside the
loop, and the loop will repeat it as necessary. The general structure
is "for every digit in num, convert digit to integer and add digit to
total." If you learn to state problems in this way, the necessary code
should flow out from it almost automatically. We can already see that
we'll need a for loop here, and a variable called total with a
sensible starting value[2]. See if you can figure it out.

We're almost done here. at the end of the loop we've got a variable
called total, with our added digits. But the point of a loop is to run
multiple times, and our loop wants to have this total in a variable
called num, and it wants it to be a string so we can get at the
individual digits again. So we want to take the integer from total,
convert it back to a string, and put it in the num variable, right at
the end of the loop. That way it can run over and over until we have
only one digit left:

no_slashes_birthday = # use replace here in someway on the birthday?
num = no_slashes_birthday
while num > 10:
    # for every digit, convert digit to int and add to total
    # convert total back to string and put it in num for the next iteration

if you fill in the blanks you'll be almost done. There is one problem
though, and it's in the condition of the while loop. The statement
"while num > 10" makes no sense at all, because num, despite its name,
is not a number but a string. Now *we* know that there are characters
representing a number inside the string, but the computer doesn't care
which characters represent a number and it shouldn't, because you've
chosen to store it as a string. So for all it cares you might as well
be asking if "hello" > 10, which very obviously doesn't make sense.
See if you can find a way around this (hint: the requirement was "keep
going while we have more than 1 digit." strings can't be compared to
numbers, but they do have a length).

And with that, you should be able to get it working. Good luck, and if
you have any trouble come back here.

Hugo

P.S.: I wrote this as soon as I woke up. Hopefully this reaches you in
time for you finals, though I fear the worst. In any case, (stating
the obvious) it might be a good idea to start studying a little
earlier next time ;) Programming isn't learned in a day.


More information about the Tutor mailing list