I have the code
num = 1
num2 = 1
num3 = 1
list = []
list2 = []
list3 = []
def numCheck1 (num):
while num<1001:
if (num%3==0):
if (num%5==0):
print num
list.append(num)
num+=1
numCheck1(num)
break
else:
print "error 1"
else:
print "error 2"
num+=1
numCheck1(num)
total=sum(list)
print list
print total
def numCheck2 (num2):
while num2<1001:
if (num2%5==0):
print num2
list2.append(num2)
num2+=1
numCheck1(num2)
break
else:
print "error"
numCheck2(num2)
def numCheck3 (num3):
while num3<1001:
if (num3%3==0):
print num3
list3.append(num3)
num3+=1
numCheck1(num3)
break
else:
print "error"
numCheck3(num3)
total2 = sum(list2)
total3 = sum(list3)
overall = (total2 + total3) - total
print list2
print list3
print total2
print total3
print overall
As a basic summary of my code, I have 3 functions, and corresponding lists and variables for each of them. The first function checks for all multiples of 3 and 5 below and equal to 1000. The second checks for all multiples of 5 below and equal to 1000. The third checks for all multiples of 3 below and equal to 1000. The numbers that are multiples are added to the corresponding list, while the corresponding variable is incremented to allow the function to check all numbers. At the end, the program calculates 4 totals: the total of each of the lists, and a special total, which adds together the second two totals and subtracts the first to prevent overcounting. This is just the overarching structure of the program.
This program is supposed to solve this problem (not homework, just fun). The code is working (as far as I know; the first function definitely works) but it keeps crashing the compiler (I am using an online compiler, repl. I am wondering if there are any ways to make this code more efficient.
Thanks!
whileis sufficient for this purposenumCheck1from inside itself. Also, don't assign a variable to the namelist(orstr, or anything else reserved). And finally, there are a lot of elements here that violate Python style, such as spacing and use of brackets - check out PEP8 for more.listas a variable name.listis a reserved word in python, e.g. list((1, 2, 3))