Creating a file that displays consecutive numbers












2















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)









share|improve this question























  • 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
















2















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)









share|improve this question























  • 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














2












2








2








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)









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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











  • 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



















  • 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

















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












1 Answer
1






active

oldest

votes


















2














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.






share|improve this answer





















  • 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











  • @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











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
});


}
});














draft saved

draft discarded


















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









2














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.






share|improve this answer





















  • 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











  • @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
















2














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.






share|improve this answer





















  • 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











  • @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














2












2








2







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








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 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






  • 1





    @AlbinPaul Finally it's good...

    – U9-Forward
    Nov 16 '18 at 5:24














  • 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











  • @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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Bressuire

Vorschmack

Quarantine