i need help i would like to convert this code to python is there anything online that will turn me into a python? from javascript? the question is how can I convert it online?
const number = parseInt(prompt('od: ')); const range = parseInt(prompt('do: ')); for(let i = 1; i <= range; i++) { const result = i * number; const result1 = result*2; function d_root(num) { return (num % 9) || 9; } if((result1.toString().length) == 1){ var z = (result*2) console.log(`${number} * ${i} = ${result},*2=vysledek,${z}`); } if((result1.toString().length) == 2){ var z = (result*2) var a =(z.toString().slice(-2,1)) var b =(z.toString().slice(-1)) console.log(`${number} * ${i} = ${result},*2=vysledek,${z}, ${a+b},${b+a}`); } if((result1.toString().length) == 3){ var z = (result*2) var a =(z.toString().slice(-0,1)) var b =(z.toString().slice(1,2)) var c =(z.toString().slice(2,3)) console.log(`${number} * ${i} = ${result},*2=vysledek,${z}, ${a+c+b},${a+b+c}`); } if((result1.toString().length) == 4){ var z = (result*2) var a =(z.toString().slice(0,-3)) var b =(z.toString().slice(1,-2)) var c =(z.toString().slice(2,-1)) var d = (z.toString().slice(3)) console.log(`${number} * ${i} = ${result},*2=vysledek,${z}, ${a+c+d+b},${a+b+c+d}`); }}
this have in python
def digit_root(n): if n == 0: return 0 return (n - 1) % 9 + 1 n = int(input("od čisla:")) m = int(input("do čisla:")) for i in range(1,m + 1): result = i*n result1 = result*2; string = (len(str(result1))) if string == 1: print(result1) if string == 2: if string == 3: if string == 4:
how do i convert slice? and change the order of the digits?
Advertisement
Answer
I believe this does what you ask. Some of this is silly. It splits the number up into digits, then prints the individual digits in the same order.
number = int(input('od: ')) nrange = int(input('do: ')) def d_root(num): return (num % 9) or 9 for i in range(nrange): result = (i+1) * number z = result * 2 zs = str(z) if z < 10: print( f"{number} * {i+1} = {result}, *2=vysledek,{z}" ) elif z < 100: a = zs[0] b = zs[1] print( f"{number} * {i+1} = {result}, *2=vysledek,{z}, {a+b},{b+a}" ) elif z < 1000: a = zs[0] b = zs[1] c = zs[2] print( f"{number} * {i+1} = {result}, *2=vysledek,{z}, {a+c+b},{a+b+c}" ) elif z < 10000: a = zs[0] b = zs[1] c = zs[2] d = zs[3] print( f"{number} * {i+1} = {result}, *2=vysledek,{z}, {a+c+d+b},{a+b+c+d}" )