0
\$\begingroup\$

The following piece of code will print a text based on the divisibility of number.
If the number is not divisible by 3, 5 and 7, then the number gets printed.

Does making use of a f-string make the type cast of integers to string implicit?

def convert(number):
    result = ""
    if (number % 3 == 0):
        result += "Pling"
    if (number % 5 == 0):
        result += "Plang"
    if (number % 7 == 0):
        result += "Plong"
    if(number % 7 != 0 and number % 5 != 0 and number%3 != 0):
        result = f{'number'}
    return result
\$\endgroup\$
  • \$\begingroup\$ There is no way that this code can work. You have a SyntaxError which errors on malformed Python syntax - before the code even runs. \$\endgroup\$ – Peilonrayz Dec 8 '19 at 13:10
  • \$\begingroup\$ @Peilonrayz I've tried it on multiple simple numbers and it errors on that too. \$\endgroup\$ – Mast Dec 8 '19 at 18:53
4
\$\begingroup\$

In programming, there are only three useful numbers, 0, 1, and many.

If you write an if-condition that repeats all the tests already done, you're doing something wrong.

If you write similar ad hoc code for three special cases, you're doing something wrong.

E.g. adding "11 Plung" will require two more lines for the special case and the "no match" case will have to be made even longer and uglier.

Write simple code for a general case, and then supply specific data as needed:

def convert(number):
    names = [ [3, "Pling"], [5, "Plang"], [7, "Plong"] ]
    result = ""
    for value,message in names:
        if not number % value: result += message
    if message == "": message = f"{number}"
    return result

for index in range(1,25): print(index, convert(index))

That way, if you later need to add the "11" case, it's a simple change, and more importantly it happens in only one place.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Comprehension can handle your parameters instead of using elif statements. Just as an example I have converted the list into key:value pairs. The if statement is only allowing things to populate the new dictionary that satisfy the argument.

names = [ [3, "Pling"], [5, "Plang"], [7, "Plong"] ]

new_dict = {i: k for i, k in names if i%i == 0}

Python will process the input in the format you want.

import string

new_string_digits = string.digits
print(type(new_string_digits), new_string_digits, new_string_digits*2)
string_to_number = int(new_string_digits)
print(type(string_to_number), string_to_number, string_to_number*2)

Notice the difference in how the compiler has processed the information:

<class 'str'> 0123456789 01234567890123456789
<class 'int'> 123456789 246913578

In the str instance it has just repeated the string input and in the int instance the number has been multiplied. I find it quite useful to convert the two because its easy to extract or plug information into without needing to use complex calculations or arguments.

| improve this answer | |
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.