Join us at IRC!
Hacking isn't just Computers & Exploits. It's a Philosophy. - Mr_Cheese
Friday, May 25, 2012
Navigation
Members Online
Total Online: 33
Web Spiders: 17
Guests Online: 30
Members Online: 3

Registered Members: 70212
Newest Member: 44556677
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

simple python program help

iantharan
Member



Posts: 27
Location: right behind you...
Joined: 14.04.08
Rank:
Hacker Level 3
Posted on 12-12-09 12:15
so i've starting learning python, and im writing a basic program that finds all the multiples of 3 and 5 between 1 and 1000, and returns the sum. Can't seem to get it to work, not sure why :angry:
heres the code

num=1
sum=0
sum1=0
a=num%3
b=num%5
while num < 1000:
if a==0:
sum=num+sum
elif b==0:
sum1=num+sum
num=num+1
print sum+sum1


Edited by iantharan on 12-12-09 12:38
Author

RE: simple python program help

stdio
Member

Posts: 375
Location: omnipresent
Joined: 06.04.08
Rank:
God
Posted on 12-12-09 12:51
iantharan wrote:
so i've starting learning python, and im writing a basic program that finds all the multiples of 3 and 5 between 1 and 1000, and returns the sum. Can't seem to get it to work, not sure why :angry:
heres the code

num=1
sum=0
sum1=0
a=num%3
b=num%5
while num < 1000:
if a==0:
sum=num+sum
elif b==0:
sum1=num+sum
num=num+1
print sum+sum1



You want your a and b inside the loop

num=1
sum=0
sum1=0
while num < 1000:
a=num%3
b=num%5
if a==0:
sum=num+sum
elif b==0:
sum1=num+sum1
num=num+1
print sum+sum1


a few more edits: your sum1=num+sum should be sum1=num+sum1

also here is a more appropriate way of doing this

sum=0
for i in range(0,1000):
if i%3==0 or i%5==0:
sum+=i
print sum


note neither one actually include 1000 which is a multiple of 5, easy enough to change though.



I'm sorry, I cant hear you over the sound of how awesome I am!


Edited by stdio on 12-12-09 14:03
www.thewebsiteisdown.com
Author

RE: simple python program help

wolfmankurd
Member



Posts: 1519
Location: UK
Joined: 30.05.05
Rank:
God
Posted on 12-12-09 16:18
iantharan wrote:
so i've starting learning python, and im writing a basic program that finds all the multiples of 3 and 5 between 1 and 1000, and returns the sum. Can't seem to get it to work, not sure why :angry:
heres the code

num=1
sum=0
sum1=0
a=num%3
b=num%5
while num < 1000:
if a==0:
sum=num+sum
elif b==0:
sum1=num+sum
num=num+1
print sum+sum1


stdio's code is idiomatic.
but think it's worth pointing out why your code failed anyways, you seem to thinkg that a=num%3 will make a into a list of variables. but it doesn't it just does a=1%3 (ie 1.)
instead, you need a re-evaluted each time you check. so you need it in the while loop!
Also, once it's there you may aswell drop the whole thing and put them in the if clauses ( as in num%3==0).

Then it's a short step (which you would have taken had you known about the or keyword) to something similar to stdio's.

Again the use of range is just know about that technique and with compound arithmetic.

On the whole good effort!


BY READING MY POST, YOU ACCEPT IT AS IS AND AGREE TO MY DISCLAIMER OF ALL WARRANTIES, EXPRESS OR IMPLIED, AS WELL AS DISCLAIMERS OF ALL LIABILITY, DIRECT, INDIRECT, CONSEQUENTIAL OR INCIDENTAL, THAT MAY ARISE FROM THE USE OF THIS (MIS)INFORMATION.




Edited by wolfmankurd on 12-12-09 16:21
Widowmakr@hotmail.com http://LetsHackStuff.com
Guest
Username

Password

Remember Me


Bookmark This Page
Affiliates
Adverts

 

 

Links
By using, viewing or obtaining any information contained on this site, you agree to the disclaimer.

© HellBound Hackers 2008- 2009. Since 3rd December 2004.