how do i make it print the multiplication table in the created file?
Question:
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.
Answers:
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.
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.
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.