Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how do i make it print the multiplication table in the created file?

I’ve been trying to put a print statement inside the loop to no avail.

def individualizing_file(number: int) -> None:
    increasing_number: int = number
    with open(f"file_{increasing_number}.txt", "w") as f:
        f.write(f"Multiplication table for + {n}")
        for _ in range(10):
                #print(n*2) 

                increasing_number += n
if __name__ == '__main__':
    n = int(input("Enter a number between 1-9: "))
    individualizing_file(n)

Tried to put a print statement in the for loop but it prints nothing inside the file_n file.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use print(file=f):

def individualizing_file(number: int) -> None:
    with open(f"file_{number}.txt", "w") as f:
        f.write(f"Multiplication table for + {n}")
        for multiple in range(10):
            print(number * multiple, file=f)

Unlike using f.write, print automatically adds a newline at the end. It also accepts any type and not just strings.

For reference to more print options see: the documentation for print. For example if you want to change the ending character or separator.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading