2

I am currently a beginner in Python. This is my problem: First, the program asks that you input a number.

For example, if I put 1, I get 1 out. If i put 2, i get 12 out. If I put 3, I get 123. If I put 4, I get 1234. That is the gist of this set of problem. However, I developed a mathematical equation that works if I put it through a loop:

if __name__ == '__main__': # ignore this part

    n = int(input())
    s = 1
    while s > n:
        z = s*10**(n-s)
        s += 1
        answer = z
        if s == n:
            print(z)

When I tried to run this code, I got nothing, even though I added print at the end. What is it that I am doing wrong here? For anyone answering the problem, introduce any concepts that you know that may help me; I want to learn it.

Please enlighten me. Don't exactly give me the answer....but try to lead me in the right direction. If I made a mistake in the code (which I am 100% sure I did), please explain to me what's wrong.

halfer
  • 19,824
  • 17
  • 99
  • 186
Kid_Vic
  • 21
  • 1

4 Answers4

1

It's because your while loop condition is backwards. It never enters the loop because s is not bigger than n. It should be while s < n

slayer
  • 643
  • 7
  • 14
1

Here is the solution:

Using string

a = int(input())

# taking the input from the user
res=''
# using empty string easy to append 
for i in range(1,a+1):
     # taking the range from 1 (as user haven't said he want 0, go up to 
     # a+1 number (because range function work inclusively and  will iterate over 
     # a-1 number, but we also need a in final output ))
     res+=str(i)
     # ^ appending the value of I to the string variable so for watch iteration 
     # number come append to it.
     # Example :  1-> 12-> 123-> 1234-> 12345-> 123456-> 1234567-> 12345678-> 123456789
     # so after each iteration number added to it ,in example i have taken a=9

sol = int(res) #converting the res value(string) to int value (as we desire)

print(sol)

In one line, the solution is

a=int(input())
res=int(''.join([str(i) for i in range(1,a+1)]))
halfer
  • 19,824
  • 17
  • 99
  • 186
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
  • you cannot use strings in this problem – Kid_Vic Dec 28 '18 at 05:19
  • unfortunately, it does not work for numbers 10 and bigger. – Kid_Vic Dec 28 '18 at 19:04
  • @Kid_Vic see the updated one. it is working for all numbers – sahasrara62 Dec 28 '18 at 19:21
  • 2
    Spoonfeeding someone a solution doesn't help them learn what they did wrong. Address *why* the original code was wrong, don't just provide an alternative that doesn't explain *why* the alternative is more correct. See also [How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) on [meta]. – Charles Duffy Dec 28 '18 at 19:37
  • @CharlesDuffy not spoonfeeding, he point out that my previous solution were wrong for certin case. so i imporved my solution for that – sahasrara62 Dec 28 '18 at 22:53
  • I wasn't referring to the comments but the answer itself -- it shows "the solution", but doesn't explain anything about *why* this code is more correct than what the OP started with, or how they should approach a problem to end with this (indeed, more correct) solution. – Charles Duffy Dec 28 '18 at 22:54
  • because i want to help him. and thanks for pointing out the how to answer hommework questions – sahasrara62 Dec 28 '18 at 22:57
  • @prashantrana.....I think this is the same one as I tried before. Explain to me the logic behind all of these code, mainly the "for" loop you got there. Before I even implement code....I want to know what it is first. – Kid_Vic Dec 29 '18 at 02:54
  • In it I have taken a empty string and using for loop I am appending (adding)the integer with the string – sahasrara62 Dec 29 '18 at 07:21
  • 1
    @halfer will try to give solution with more explanation from now on. – sahasrara62 Dec 29 '18 at 19:05
1

Use a for loop using range() as in

for i in range(1, n+1):

where n is the input so that the numbers from 1 till n can be obtained.

Now use a print() to print the value of i during each iteration.

print() by default will add a newline at the end. To avoid this, use end argument like

print(var, end='')

Once you are familiar with this, you can also use list comprehension and join() to get the output with a single statement like

print( ''.join([str(i) for i in range(1, n+1)]) )

Take the input as you've done using input() and int() although you might want to include exception handling in case the input is not an integer.

See Error handling using integers as input.

J...S
  • 5,079
  • 1
  • 20
  • 35
0

Try this,

n = int(input('Please enter an integer'))
s = 1
do:
    print(s)
    s+=1
while s == n

this works. (Simple and Short)