+ 2

Help with code

Hi, im trying to write program that do following: input(n) print 1234....n without using string methods i make this code but something doesnt go as planned and i dont know what it is. if __name__ == '__main__': n = int(input()) answer = 0 for i in range(n+1): x = i % 10 answer = answer * 10 answer =+ x i =+ 1 print(answer) as you can see i add the answer * 10 to "move the number to the left", but it looks like it does nothing

4th Jan 2022, 12:10 PM
KryĆĄtof VĂĄvra
KryĆĄtof VĂĄvra - avatar
4 Answers
+ 5
You could do it like this for i in range(1,int(input())+1): print(i,end ="")
4th Jan 2022, 1:45 PM
Simba
Simba - avatar
+ 3
# Use this instead answer += x i += 1
4th Jan 2022, 12:20 PM
SoloProg
SoloProg - avatar
+ 2
OMG such a small mistake :D i hate my Dyslexia thx :D the problem was i had =+ instead of +=
4th Jan 2022, 12:22 PM
KryĆĄtof VĂĄvra
KryĆĄtof VĂĄvra - avatar
0
Also it work only till input = 9 :D so now the new task begin :D Edit: This is how it works now for numbers smaller that 1000. I just need input in range(150). :) if __name__ == '__main__': n = int(input()) answer = 0 for i in range(n+1): x = i % 10000 if i < 10: answer = answer * 10 elif 10 <= i <100: answer = answer * 100 else: answer = answer * 1000 answer += x i += 1 print(answer)
4th Jan 2022, 12:26 PM
KryĆĄtof VĂĄvra
KryĆĄtof VĂĄvra - avatar