Jump to content

Code not running!! Please help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
king_koder

king_koder

    Newbie

  • Members
  • Pip
  • 7 posts
Hi,
I tried running the following code in IDLE (3.1.2) and it just closed after running some time. Could you guys help me out with this. I also tried running the program in Geany and there was the same problem.

a=100

b=100

great=0

multiples=[]

palin=[]


#create nested loops to create list of multiples of all three digit numbers.


while a<1000:

    while b<1000:

        multiples.append(a*b)

    b+=1

a+=1


for n in  multiples:

    if str(n)==str(n)[::-1]:

        palin.append(n)


for a in  palin:

    if a>great:

        great=a


print(great)

The code is supposed to find the greatest palindromic number formed by the multiplication of two 3-digit numbers.

#2
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
You've made two infinite loops, b will always be < 1000 and a will always be < 1000. You have to move the increasing part one step further to the right.

#3
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Instead of using while loop, which is unpythonic and slow for some reason, you have to use this construction in Python:


for a in range(1000):

    print(a)


This will make your code more clear and prevent from typical mistakes (infinitive loop in your case).

#4
BrockyL

BrockyL

    Newbie

  • Members
  • Pip
  • 2 posts
if str(n) == str(n)[::-1]:
this is to say compare a string (ex:'abc') to itself backwards which looking up the first bit which is probably a palindrome itself stored on the harddrive. is not going to ever equal itself backwards.
simply multiplex the two lists like you did in the first loop, or even more tricky there is a list lookup feature of python eg:
if (str(n)[::-1] in otherlist):
print str(n)+" and "+str(otherlist[otherlist.index(str(n)[::-1])])+" are palindromes."
requiring only one loop. :)