2
\$\begingroup\$

This is just a little palindromic game that searches all palindromic numbers in ur random generated list. Feel free to give me some advice.

import random

numbers = []
total = []
dice = random.randint(1000, 50000)


while len(numbers) < dice:
        x = random.randint(100000, 1000000)
        numbers.append(x)


def palindrome(n):
    check_palin = []
    for i in n[::-1]:
        i = str(i)
        check_palin.append(i)
        for k in check_palin:
            k = k[::-1]
            check_palin.clear()
            if k == i:
                total.append(k)
    print(total)
    numbers.clear()


palindrome(numbers)

length = len(total)


if length > 58:
    print("\nHolly Shit!!! You broke my record!!! with %d." % (length))

elif length == 58:
    print("\nWow, Nice!!! %d is my highest Palindromic number (too).\nThat's a draw" % (length))
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Instead of saving len(total), just use:

if len(total) > 58:
     print("\nHolly Shit!!! You broke my record!!! with %d." % (length))

elif len(total) == 58:
    print("\nWow, Nice!!! 58 is my highest Palindromic number (too).\nThat's a draw") 
    # Since you hardcoded your -highscore-, there's no need to use a variable to print the highscore.

Use whitespace more efficiently, especially in your palindrome() function. Instead of converting n to str() in your palindrome() function, use numbers.append(str(x)). This way you can reuse palindrome() and have it take n as a string by default (or use an assertion / try: except: statement).

Rewritten:

from random import randint

numbers = []
palindromes = []
dice = randint(1000, 50000)

while len(numbers) < dice:
    x = randint(100000, 1000000)
    numbers.append(str(x))


def palindrome(n):
    for i in n[::-1]:
        if i == i[::-1]:
            palindromes.append(i)
    return palindromes


palindromes = palindrome(numbers)
print(palindromes) # To check this works

if len(palindromes) > 58:
    print("\nHoly shit! You broke my record with %d!\n" % (length))

elif len(palindromes) == 58:
    print("\nWow, nice! 58 is my highest palindromic number too!\n")
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.