Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Write a function that takes, as an argument, a positive integer n, and create

ID: 3810521 • Letter: 2

Question

2. Write a function that takes, as an argument, a positive integer n, and creates a file, named eightBits.txt, containing n randomly generated eight-bit binary strings, each on its own line. Examples of eight-bit binary strings include “10001111” and “00110011”. Name this function eightBitStrings(n).

3. Write a function that takes, as an argument, a positive integer n, and creates a file, named nLinesOfText.txt, containing n lines of text obtained through keyboard input. In other words, the function should prompt the user to enter text n times, and it should write that information to a file. Name this function userInput(n).

4. Write a function that involves no arguments, named getAllTheText(), that prompts the user to enter text. If the user enters the word QUIT (in all uppercase), the function ends (closing any files that were opened at any point during this function’s operation). If the text entered is not QUIT, the function writes the text entered to a file named unlimitedText.txt.

5. Write a function that involves no arguments, named makeStrings(), that prompts the user to 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 kbit 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

# importing random to have random numbers
import random

# funciton that is taking n as argument
def eightBitStrings(n):
# opening a file which means creating if it doesn't exist and opening it in writing mode
file = open("eightbits.txt","w")
  
# for n number of times
for i in range(n):
  
# storing 8 bit binary in ebit
ebit = ""
  
# for eight times, generating binary numbers
for j in range(8):
rand = random.randrange(2) # gives 0/1
  
# adding to the bit
if rand == 0:
ebit = ebit + "0"
else:
ebit = ebit + "1"
  
# writing that ebit to file with new line character
file.write(ebit)
file.write(" ")
  
# finally closing once done with n eightbits
file.close()

eightBitStrings(8) # test

-- Sample Output

11100001
01101110
00100010
11101010
01101010
00011000
00000010
00100010

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote