Begginers Guide - Exrcise. Help me, plz!

Marco Herrn herrn at gmx.net
Sat Mar 16 16:02:49 EST 2002


Hi,

On Sat, Mar 16, 2002 at 11:45:30PM +0300, Jeffrey-man wrote:
> Learning the Begginers Guide, I found an exercise. It tells me:
> "Write a program that continually reads in numbers from the user and adds
> them together until the sum reaches 100."
> Can anybody help me?
> 
>  -----
> 
> sum_stop = 100
> num = input("Please enter the number: ")
> 
> for sum in range(num+num,sum_stop):
>     print "Now the sum is", num+num
>     sleep(1)
> 
> print "The sum is >/= 100!"
> 
>  -----
> 
> Maybe I'm doing something wrong?

You should tell what exactly your problem is ;)
But why so complicated? I would do the following construct:

---/---
>>> sum= 0
>>> sum_stop= 100
>>> while sum < 100:
...   num= input("Please enter the number: ")
...   sum= sum+num
...   print ("The sum is now "), sum
...   print "The sum is >/= 100!"
---/---

What is different here is, instead of the for-loop i use a while loop,
which seems more appropriate to me, because the programm should go
through the loop _until_ 100 is reached.

There are some other things that are a bit strange in your code. Maybe
you write a mail and explain what you wanted to do in each line.

Bye
Marco
-- 
Who will take care of the world after you're gone?

--
Marco Herrn             herrn at gmx.net
(GnuPG/PGP-signed and crypted mail preferred)
Key ID: 0x94620736




More information about the Python-list mailing list