Creating a file that displays consecutive numbers
I have to create a text file that will display a consecutive number each line
consecutive_numbers("numbers.txt", 5) should display 1-5 on different lines each
for some program will not run.
def consecutive_numbers(filename,n):
"""
sig : str , int -> NoneType
"""
myfile = open(filename, "w")
for i in range(n):
newline = i + 1
myfile.write(str(newline) + "n")
myfile.close()
#consecutive_numbers("numbers.txt", 5)
python
add a comment |
I have to create a text file that will display a consecutive number each line
consecutive_numbers("numbers.txt", 5) should display 1-5 on different lines each
for some program will not run.
def consecutive_numbers(filename,n):
"""
sig : str , int -> NoneType
"""
myfile = open(filename, "w")
for i in range(n):
newline = i + 1
myfile.write(str(newline) + "n")
myfile.close()
#consecutive_numbers("numbers.txt", 5)
python
For me its working.
– Rohit-Pandey
Nov 16 '18 at 5:05
when i run the code, nothing shows up, I didn't know it worked. Why am i not seeing anything in the console
– jfisk
Nov 16 '18 at 5:09
Checknumbers.txt
named file inside the same directory where program resides.
– Rohit-Pandey
Nov 16 '18 at 5:11
@jfisk View my answer, to see the better code...
– U9-Forward
Nov 16 '18 at 5:14
add a comment |
I have to create a text file that will display a consecutive number each line
consecutive_numbers("numbers.txt", 5) should display 1-5 on different lines each
for some program will not run.
def consecutive_numbers(filename,n):
"""
sig : str , int -> NoneType
"""
myfile = open(filename, "w")
for i in range(n):
newline = i + 1
myfile.write(str(newline) + "n")
myfile.close()
#consecutive_numbers("numbers.txt", 5)
python
I have to create a text file that will display a consecutive number each line
consecutive_numbers("numbers.txt", 5) should display 1-5 on different lines each
for some program will not run.
def consecutive_numbers(filename,n):
"""
sig : str , int -> NoneType
"""
myfile = open(filename, "w")
for i in range(n):
newline = i + 1
myfile.write(str(newline) + "n")
myfile.close()
#consecutive_numbers("numbers.txt", 5)
python
python
asked Nov 16 '18 at 5:00
jfiskjfisk
241
241
For me its working.
– Rohit-Pandey
Nov 16 '18 at 5:05
when i run the code, nothing shows up, I didn't know it worked. Why am i not seeing anything in the console
– jfisk
Nov 16 '18 at 5:09
Checknumbers.txt
named file inside the same directory where program resides.
– Rohit-Pandey
Nov 16 '18 at 5:11
@jfisk View my answer, to see the better code...
– U9-Forward
Nov 16 '18 at 5:14
add a comment |
For me its working.
– Rohit-Pandey
Nov 16 '18 at 5:05
when i run the code, nothing shows up, I didn't know it worked. Why am i not seeing anything in the console
– jfisk
Nov 16 '18 at 5:09
Checknumbers.txt
named file inside the same directory where program resides.
– Rohit-Pandey
Nov 16 '18 at 5:11
@jfisk View my answer, to see the better code...
– U9-Forward
Nov 16 '18 at 5:14
For me its working.
– Rohit-Pandey
Nov 16 '18 at 5:05
For me its working.
– Rohit-Pandey
Nov 16 '18 at 5:05
when i run the code, nothing shows up, I didn't know it worked. Why am i not seeing anything in the console
– jfisk
Nov 16 '18 at 5:09
when i run the code, nothing shows up, I didn't know it worked. Why am i not seeing anything in the console
– jfisk
Nov 16 '18 at 5:09
Check
numbers.txt
named file inside the same directory where program resides.– Rohit-Pandey
Nov 16 '18 at 5:11
Check
numbers.txt
named file inside the same directory where program resides.– Rohit-Pandey
Nov 16 '18 at 5:11
@jfisk View my answer, to see the better code...
– U9-Forward
Nov 16 '18 at 5:14
@jfisk View my answer, to see the better code...
– U9-Forward
Nov 16 '18 at 5:14
add a comment |
1 Answer
1
active
oldest
votes
Why not just:
def consecutive_numbers(filename,n):
"""
sig : str , int -> NoneType
"""
with open(filename,'w') as f:
f.write('n'.join(range(1,n+1)))
Actually your code works for me, but to make it little more efficient and shorter.
1
this puts only n-1 elements into the filerange(1,n)
. putrange(1,n+1)
– Albin Paul
Nov 16 '18 at 5:23
@AlbinPaul Oh maan... My Bad... My Mistake..., i'll edit
– U9-Forward
Nov 16 '18 at 5:24
1
@AlbinPaul Finally it's good...
– U9-Forward
Nov 16 '18 at 5:24
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331711%2fcreating-a-file-that-displays-consecutive-numbers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Why not just:
def consecutive_numbers(filename,n):
"""
sig : str , int -> NoneType
"""
with open(filename,'w') as f:
f.write('n'.join(range(1,n+1)))
Actually your code works for me, but to make it little more efficient and shorter.
1
this puts only n-1 elements into the filerange(1,n)
. putrange(1,n+1)
– Albin Paul
Nov 16 '18 at 5:23
@AlbinPaul Oh maan... My Bad... My Mistake..., i'll edit
– U9-Forward
Nov 16 '18 at 5:24
1
@AlbinPaul Finally it's good...
– U9-Forward
Nov 16 '18 at 5:24
add a comment |
Why not just:
def consecutive_numbers(filename,n):
"""
sig : str , int -> NoneType
"""
with open(filename,'w') as f:
f.write('n'.join(range(1,n+1)))
Actually your code works for me, but to make it little more efficient and shorter.
1
this puts only n-1 elements into the filerange(1,n)
. putrange(1,n+1)
– Albin Paul
Nov 16 '18 at 5:23
@AlbinPaul Oh maan... My Bad... My Mistake..., i'll edit
– U9-Forward
Nov 16 '18 at 5:24
1
@AlbinPaul Finally it's good...
– U9-Forward
Nov 16 '18 at 5:24
add a comment |
Why not just:
def consecutive_numbers(filename,n):
"""
sig : str , int -> NoneType
"""
with open(filename,'w') as f:
f.write('n'.join(range(1,n+1)))
Actually your code works for me, but to make it little more efficient and shorter.
Why not just:
def consecutive_numbers(filename,n):
"""
sig : str , int -> NoneType
"""
with open(filename,'w') as f:
f.write('n'.join(range(1,n+1)))
Actually your code works for me, but to make it little more efficient and shorter.
edited Nov 16 '18 at 5:24
answered Nov 16 '18 at 5:05
U9-ForwardU9-Forward
16.9k51643
16.9k51643
1
this puts only n-1 elements into the filerange(1,n)
. putrange(1,n+1)
– Albin Paul
Nov 16 '18 at 5:23
@AlbinPaul Oh maan... My Bad... My Mistake..., i'll edit
– U9-Forward
Nov 16 '18 at 5:24
1
@AlbinPaul Finally it's good...
– U9-Forward
Nov 16 '18 at 5:24
add a comment |
1
this puts only n-1 elements into the filerange(1,n)
. putrange(1,n+1)
– Albin Paul
Nov 16 '18 at 5:23
@AlbinPaul Oh maan... My Bad... My Mistake..., i'll edit
– U9-Forward
Nov 16 '18 at 5:24
1
@AlbinPaul Finally it's good...
– U9-Forward
Nov 16 '18 at 5:24
1
1
this puts only n-1 elements into the file
range(1,n)
. put range(1,n+1)
– Albin Paul
Nov 16 '18 at 5:23
this puts only n-1 elements into the file
range(1,n)
. put range(1,n+1)
– Albin Paul
Nov 16 '18 at 5:23
@AlbinPaul Oh maan... My Bad... My Mistake..., i'll edit
– U9-Forward
Nov 16 '18 at 5:24
@AlbinPaul Oh maan... My Bad... My Mistake..., i'll edit
– U9-Forward
Nov 16 '18 at 5:24
1
1
@AlbinPaul Finally it's good...
– U9-Forward
Nov 16 '18 at 5:24
@AlbinPaul Finally it's good...
– U9-Forward
Nov 16 '18 at 5:24
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331711%2fcreating-a-file-that-displays-consecutive-numbers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
For me its working.
– Rohit-Pandey
Nov 16 '18 at 5:05
when i run the code, nothing shows up, I didn't know it worked. Why am i not seeing anything in the console
– jfisk
Nov 16 '18 at 5:09
Check
numbers.txt
named file inside the same directory where program resides.– Rohit-Pandey
Nov 16 '18 at 5:11
@jfisk View my answer, to see the better code...
– U9-Forward
Nov 16 '18 at 5:14