-1
import random
class simulate_DNA:
    def create_DNA(length):
        sequence = ""
        for i in range(length):
            sequence = sequence + random.choice("ATGC")
        return print(sequence)
def main():
    length = 10
    output_file = input("Enter output file path and name: ")
    output_file = open(output_file, "w")
    for i in range(10):
        # simulate_DNA.create_DNA(length)
        output_file.write(simulate_DNA.create_DNA(length))
        output_file.readline()
    output_file.close()
if __name__ == '__main__':
    main()

I got this error after running the code above: TypeError: write() argument must be str, not None Would anyone please tell me how to fix this error? Thank you so much!

SuperStormer
  • 4,997
  • 5
  • 25
  • 35
Chris
  • 37
  • 6

1 Answers1

1

The print function returns None, and you're trying to return the result of the print function => So it's None

Replace this: return print(sequence) By: return sequence

PySoL
  • 566
  • 3
  • 9
  • Would you please tell me why return print(sequence) doesn't print a random string? – Chris Feb 17 '22 at 01:50
  • 1
    The `print(sequence)` does print the random string. But it returns `None` instead of the random string you expected. So this lead to the error at this line `output_file.write(simulate_DNA.create_DNA(length))` because you're trying to do `output_file.write(None)`. This is illegal as file writer only allows the string and integer but None. – PySoL Feb 17 '22 at 02:28
  • So why does simulate_DNA.create_DNA(length) create None in this case? – Chris Feb 17 '22 at 12:23
  • 1
    Because the function `create_DNA` returns `print(...)` which is none. – PySoL Feb 17 '22 at 14:08