How do i write the integers that in this file to the mynumbers.txt file?

phillip swag

Ok this function works perfectly, I just need to know how do i make it so it so the random integers that are generated, are written to the mynumbers.txt file.

def main():

#Open a file for writing
 outfile = open ('mynumbers.txt' , 'w')


#Generate random integer number
from random import randint
number = randint(6, 12)
print("Write will create", number, "random numbers")

import random

i = 0
while i < 11:
# Get random number in range 10 through 20.
n = random.randint(10, 20)
print(n)
i += 1
#Write the numbers to the file

foo = ""
while (i):
#find n
foo += str(n) + " "
outFile.write(foo)

#Call the main function
main()

Is this right?

Mortuus

Try opening the file with the append option instead: outfile=Open("[FileName]", "a") Then you can do a outfile.write(str(n) + " ") where n is the number you wish to write. Of course you can pick a deliminator other than " ".

Alternatively, you could store all of the numbers in a string. And write them all at the end.

foo = ""
while (looping):
    #find n
    foo += str(n) + " "
outFile.write(foo)

===REVISED===

import random

output = ""
deliminator = "\n" #Put a line-break after each number
filepath = "mynumbers.txt"
i = 0
while i < 11:
# Get random number in range 10 through 20.
    n = random.randint(10, 20)
    print(n) #is this necessary?
    i += 1
    output += str(n) + deliminator

#Write the numbers to the file
with open(filepath, 'w') as file:
    file.write(output)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How do I write a custom module for AngularJS?

来自分类Dev

How do I create a link to a pdf file with rails?

来自分类Dev

Editing a txt file with PHP/html

来自分类Dev

Passing File with intent, how do i retrieve it

来自分类Dev

How do I open an outlook .msg file from my harddrive that is NOT in outlook?

来自分类Dev

How do I parse Json with an invalid character for a field name? e.g. { "file/folder": "/Shared/Salesforce/asdf.txt" } with Newtonsoft?

来自分类Dev

how to open existing file like .docx, .txt, .pptx through java?

来自分类Dev

How do I iterate over a file?

来自分类Dev

How do I find the path of a data file in Dancer to use in Javascript?

来自分类Dev

GIT: How do I add a file to the first commit (and rewrite history in the process)?

来自分类Dev

Write binary file in TideSDK

来自分类Dev

How do I make a bitmap version of a WMF file that is loaded into a TImage.Picture and move that to a TSpeedButton.Glyph

来自分类Dev

How to write unit test for file upload class in django?

来自分类Dev

How do I get Ruby to search for a pattern on the tail of a local file?

来自分类Dev

How to write integers to port using PySerial

来自分类Dev

Write text to file in columns

来自分类Dev

How to write d.ts file from existing .js file?

来自分类Dev

How do I print out just certain elements from a text file that has xml tags to a new text file?

来自分类Dev

How do I save a JSON file with four spaces indentation using JSON.NET?

来自分类Dev

How to write in ARFF file using LIAC-ARFF package in Python?

来自分类Dev

When using (substack's) Tape module for testing, how do I run only one test in a file?

来自分类Dev

为什么在git diff输出示例中,我看到a / file.txt b / file.txt但实际上我看到了i / file.txt

来自分类Dev

如何将此文件中的整数写入mynumbers.txt文件?

来自分类Dev

How do I add file paths as nodes to a tree or stack in C++

来自分类Dev

带有>>的powershell Write-Output到file.txt-附加

来自分类Dev

How do I open a VBA file in notepad or notepad++

来自分类Dev

为什么在fopen('file.txt','r +')之后是write()截断文件?

来自分类Dev

为什么在git diff输出示例中,我看到a / file.txt b / file.txt,但实际上我看到了i / file.txt

来自分类Dev

命令:tail file.txt '

Related 相关文章

  1. 1

    How do I write a custom module for AngularJS?

  2. 2

    How do I create a link to a pdf file with rails?

  3. 3

    Editing a txt file with PHP/html

  4. 4

    Passing File with intent, how do i retrieve it

  5. 5

    How do I open an outlook .msg file from my harddrive that is NOT in outlook?

  6. 6

    How do I parse Json with an invalid character for a field name? e.g. { "file/folder": "/Shared/Salesforce/asdf.txt" } with Newtonsoft?

  7. 7

    how to open existing file like .docx, .txt, .pptx through java?

  8. 8

    How do I iterate over a file?

  9. 9

    How do I find the path of a data file in Dancer to use in Javascript?

  10. 10

    GIT: How do I add a file to the first commit (and rewrite history in the process)?

  11. 11

    Write binary file in TideSDK

  12. 12

    How do I make a bitmap version of a WMF file that is loaded into a TImage.Picture and move that to a TSpeedButton.Glyph

  13. 13

    How to write unit test for file upload class in django?

  14. 14

    How do I get Ruby to search for a pattern on the tail of a local file?

  15. 15

    How to write integers to port using PySerial

  16. 16

    Write text to file in columns

  17. 17

    How to write d.ts file from existing .js file?

  18. 18

    How do I print out just certain elements from a text file that has xml tags to a new text file?

  19. 19

    How do I save a JSON file with four spaces indentation using JSON.NET?

  20. 20

    How to write in ARFF file using LIAC-ARFF package in Python?

  21. 21

    When using (substack's) Tape module for testing, how do I run only one test in a file?

  22. 22

    为什么在git diff输出示例中,我看到a / file.txt b / file.txt但实际上我看到了i / file.txt

  23. 23

    如何将此文件中的整数写入mynumbers.txt文件?

  24. 24

    How do I add file paths as nodes to a tree or stack in C++

  25. 25

    带有>>的powershell Write-Output到file.txt-附加

  26. 26

    How do I open a VBA file in notepad or notepad++

  27. 27

    为什么在fopen('file.txt','r +')之后是write()截断文件?

  28. 28

    为什么在git diff输出示例中,我看到a / file.txt b / file.txt,但实际上我看到了i / file.txt

  29. 29

    命令:tail file.txt '

热门标签

归档