Replacing numbers ending in 3 and 7 in a string
<p>Write a program that generates and prints a list of n elements (n informed by the user) containing the natural numbers (starting with 1) and replacing multiples of 3 by the word 'ping', multiples of 7 by the word 'pong', and multiples of 3 and 7 by the word 'ping-pong'</p>

<p>Here is the code for that</p>

<pre><code>result = []
number = eval(input("Enter a whole number: "))
for index in range(number):
    if index % 7 == 0 and index % 3 == 0:
        result.append("ping-pong")
    elif index % 3 == 0:
        result.append("ping")
    elif index % 7 == 0:
        result.append("pong")
    else:
        result.append(index)
print(result) == 0
</code></pre>

<p>Now also replaces numbers ending in 3 by the word âPINGâ and numbers ending in 7 by the word âPONGâ this I am not sure how to go about doing.</p>

<p>I tried to make your code do what you want while doing as few modifications to it as possible.</p>

<ul>
<li>Do <strong>NOT</strong> use <code>eval</code>. Ever. Bad, bad, bad <code>eval</code>. To cast an string to an int, use <code>int()</code>.</li>
<li>Your code was starting at 0 when it was asked that it started at 1, I
changed the range.</li>
<li>To know the last digit, I calculated the number modulo 10, based on the clever comment by @Renuka Deshmukh. Other less clever solutions could have been to check the end of the number casted as a string, with <code>str(index).endswith("7")</code> or <code>str(index)[-1] == "7"</code>, for example.</li>
<li>What was your <code>print(result) == 0</code> trying to do? I removed the <code>==0</code>.</li>
</ul>

<p>Here is the resulting code:</p>

<pre><code>result = []
number = int(input("Enter a whole number: "))
for index in range(1,number+1):
    if index % 7 == 0 and index % 3 == 0:
        result.append("ping-pong")
    elif index % 3 == 0 or index % 10 == 3:
        result.append("ping")
    elif index % 7 == 0 or index % 10 == 7:
        result.append("pong")
    else:
        result.append(index)
print(result)
</code></pre>

python