I’m doing the challenges on Project Euler to get better at python (I am a beginner) and I am trying to do Problem 9.
vara = lista[-1]
varb = listb[-1]
varc = listc[-1]
while not(vara < varb < varc):
listb.pop(-1)
lista.pop(-1)
lista.pop(-1)
if ((vara * vara) + (varb * varb) == varc * varc):
print vara, varb, varc
else:
listc.pop(-1)
if ((vara * vara) + (varb * varb) == varc * varc):
print vara, varb, varc
else:
listb.pop(-1)
if ((vara * vara) + (varb * varb) == varc * varc):
print vara, varb, varc
else:
lista.pop(-1)
I have tried many methods to get lista/listb/listc to have the values from 1 – 1000 but none of them really seem to work
lista = range(1, 1000)
lista = range[1, 1000]
etc
To generate values from 1 to 1000 you need to specify the ending value as 1000 + 1, i.e., 1001. So
Python 2.x
Python 3.x
will give you a list of values from 1 to 1000.
range() uses a half-closed interval, meaning it includes the first value in the list of values it generates, but excludes the last one. Note that if you don’t specify a starting value,
range()will default to0. You can also specify an increment value, which defaults to1unless otherwise specified.Three examples from the doc page: