'Using a for loop to generate a string of numbers out of a range [closed]
I need to generate a string of the numbers from 1 to 50 with a for-loop.
my suggestion:
solution = range(1,50)
for i in solution
print(i++, end ="")
it shows me invalid syntax and I don't really get behind the reason. The output should be a string with the number from 1 to 50 added like this (12345....).
How do I do that?
Solution 1:[1]
variable++is not Python syntax at all. You might know it from C, C++, Java, JavaScript, or any of the dozens of languages that use it.- You're missing a colon after the for loop line.
The Python interpreter should have pointed out at least the latter to you. The former is easily found by Googling.
The following should give the output you need, but know that there are easier ways to do this in Python.
solution = range(1,50)
string_variable = "" # the i from below will be of type integer, not string.
for i in solution:
string_variable += str(i) # += performs appending (or addition depending on the data type), but you'll need to convert i to a string
print(string_variable)
Explanation (method 1)
Breaking down the problem
Let's think about what you want.
- You want to consider numbers from 1 to 50.
- You then want to think about them one at a time.
- While doing step 2, you want to maintain a "joined" record somewhere, and "join" each number at the end of the record as you consider it.
- Finally, you want to output that "joined" record.
Converting ideas to code
Let's go through each step one at a time, this time in code.
Consider numbers from 1 to 50.
You got this! You saved a
range()generator to a variable with 1 and 50 as its endpoints.solution = range(1, 50)Think about them one at a time.
You got this too! By using a
forloop to iterate over your generator, you are doing this.for i in solution:Join the numbers
This step gotcha :(
To understand this, you need to know about something called data types.
The
range()generator gives you integer values. Think of this as a proper value: a number that you can manipulate (add, subtract, multiply etc).However, when you want to join stuff together (called concatenating), you need to use strings.
To you and me, a number on a piece of paper is same as the number on a calculator. To a computer, it's not. If it's a string, it's treated equivalently to an alphabet: you cannot do math with it, you can only do stuff like displaying it.
To concatenate, you must convert to a string. That's why I start with a blank string variable.
string_variable = ""We do this with the following syntax:
str(i)The
strstands for string.The joining part happens with the
+=operator:string_variable += str(i)Output the string
I think you know how to do this. Just print it out:
print(string_variable)
Putting it all together:
solution = range(1,50)
string_variable = ""
for i in solution:
string_variable += str(i)
print(string_variable)
Explanation (method 2)
I think your logic was right.
solution = range(1,50)
for i in solution
print(i++, end ="")
- We're removing the
++because it isn't valid Python syntax. This is the increment operator from languages like C++. In practice, if you were using something like a while loop, you would use this to increase the value ofiby 1 in each pass of the loop. However, for loops sort of have this idea built into them, so we don't need it. - Addition of
:and indentation by 4 spaces are done because that's way we communicate to Python that the lineprint(i, end ="")is to be executed inside the loopfor i in solution.
Corrected code:
solution = range(1,50)
for i in solution:
print(i, end ="")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
