in Python, Write a function that involves no arguments, named makeStrings(), tha
ID: 3818447 • Letter: I
Question
in Python, Write a function that involves no arguments, named makeStrings(), that prompts the userto enter three pieces of information: the number of bits in each binary string (k), the number of binary strings to create (n), and the name of the file. The function should then create a file with the specified name, and then proceed to create n randomly generated k-bit binary strings and write them to the file, with each k-bit binary string on its own line.At the end of the function, the file should be closed.
Explanation / Answer
# pastebin link for code : https://pastebin.com/V4YZGdLi
from random import randint
def generateBinaryString(k):
str_list = []
for i in range(0, k):
str_list.append(str(randint(0, 1)))
return "".join(str_list)
def makeStrings():
k = int(input("Enter the number of bits in each binary string: "))
n = int(input("Enter the number of binary strings to create: "))
name = input("Enter name of file: ")
with open(name, "w") as fw:
for i in range(0, n):
fw.write(generateBinaryString(k))
fw.write(" ")
if __name__ == '__main__':
makeStrings()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.