-3

This is the code

import datetime

mylist = []
today = datetime.date.today()
mylist.append(today)
print(mylist[0])

file = open('Date.txt', 'a')
file.write(mylist)
file.close()
global timestable
global text
def timestable():
    global name
    name = input("What is your name? ")
    global number
    number = int(input("Insert a number you want the 12 times table for: "))
    global multiply
    for multiply in range(1,13):
        print(number,"x",multiply,"=",number*multiply)
        text()

def text():
    file=open("list.csv","a")
    file.write("The date is," + today + "\nTimestables are:\n" + number + "x" + multiply + "=" + number*multiply + "\nYour name is, " + name)
    file.close()
    text()
timestable()

The problem is that nothing is being outputted into the file and its supposed to be outputted with a certain format also.

The error is

Traceback (most recent call last):
  File "D:\Python\Code\Lesson Task Code.py", line 9, in <module>
    file.write(mylist)
TypeError: must be str, not list
Xcodian Solangi
  • 2,342
  • 5
  • 24
  • 52
MihirB
  • 1
  • 2
  • 2
    The error tells you what to do! Fastest way is `file.write(str(mylist))` or you use `join` to write a nice output (http://stackoverflow.com/questions/12453580/concatenate-item-in-list-to-strings) – ppasler Jan 08 '17 at 19:18
  • 2
    the python [`csv` module](https://docs.python.org/3/library/csv.html) may also come in handy... – hiro protagonist Jan 08 '17 at 19:22
  • "supposed to be outputted with a certain format". What's the format? What you are writing in `text()` is nothing like a CSV format, plus it will infinitely recurse and crash once you fix your current error. – Mark Tolonen Jan 08 '17 at 20:34

1 Answers1

0

Instead of datetime.date.today() you probably want to tryout time.strftime("%Y-%m-%d"):

from time import strftime

today = strftime("%Y-%m-%d")
print(today)

with open('Date.txt', 'a') as file:
    file.write(today+"\n")
Marvo
  • 523
  • 7
  • 16