I was just trying to create a loop within a function that performs a start stop step procedure with 3 function parameters. So, for example if the function is called with numbers 10, 20, 3, then the program will write 10, 13, 16, 19
I also want to write the results to a newly made text file.
This is what I have so far:
ranges = int(input("Enter a range to start: "))
step = int(input("Enter a step: "))
stop = int(input("Enter a stop: "))
def threeparameters(ranges,stop,step):
for i in range(ranges,stop, step):
with open("sequences.txt", "a") as f:
i = str(i)
f.write(ranges,stop,step)
f.close()
i = threeparameters(ranges,step,stop)
CodePudding user response:
to fix this the first issue is the fact that f.write() can only take one argument and so this will not work. Secondly you do not need to close the file using the with open() syntax. Another thing to note is that you are using the append mode ("a") when opening the file.
At the moment, you are opening the file within the iteration of your list, but this should be the other way round. Assuming you want to get the sequence of numbers into a new txt file on new lines, try something as follows:
def threeparameters(ranges, stop, step):
with open("sequences.txt", "w") as f:
for i in range(ranges, stop, step):
i = str(i)
f.write(i "\n")
This way the function begins by opening the file (in write mode), you then iterate through the range of numbers given by your three arguments, then convert each to string, and write it to the file with a newline "\n"
after each one. With values of range = 0
, stop = 10
, and step = 2
, the following txt file is saved:
0
2
4
6
8
CodePudding user response:
First, let's analyze your code.
ranges = int(input("Enter a range to start: "))
step = int(input("Enter a step: "))
stop = int(input("Enter a stop: "))
def threeparameters(ranges,stop,step):
for i in range(ranges,stop, step):
with open("sequences.txt", "a") as f:
i = str(i)
f.write(ranges,stop,step)
f.close()
i = threeparameters(ranges,step,stop)
There are some things I would point here:
- You are not using
i
for anything in thefor
loop. threeparameters()
is not returning any value, is trying to write integers (which produces an error) and passed many args tof.write
(another error), and is not printing anything.
So, something we can do is:
- Don't pass
ranges, stop, step
tof.write
. I suppose you want a10, 13, 16, 19
result in your file (following your example), so you can do this:
f.write(i "\n") # the newline is just to be fancier
The
f.close
step is not necessary (in awith
statement,open()
will close the file when the statement finishes).Print the
i
result on your screen, if you want:
for i in range(ranges,stop, step):
with open("sequences.txt", "a") as f:
i = str(i)
f.write(i "\n")
print(i)
- If you want to store something, add a
return whatever_you_want
at the end of thethreeparameters
function. I didn't made that change in the code, because I don't know what did you want to get.
The final code will look like this:
ranges = int(input("Enter a range to start: "))
step = int(input("Enter a step: "))
stop = int(input("Enter a stop: "))
def threeparameters(ranges,stop,step):
for i in range(ranges,stop, step):
with open("sequences.txt", "a") as f:
i = str(i)
f.write(i "\n")
print(i)
# if you want to return something, put it here
# Since we didn't return something at `threeparameters`,
# we can just call the function without storing anything.
threeparameters(ranges, step, stop)
A demo of inputs/outputs:
Enter a range to start: 10
Enter a step: 20
Enter a stop: 3
10
13
16
19
Your sequences.txt
file will contain:
13
16
19