将此文件写入文件的正确语法是什么?

时间:2014-03-14 17:36:52

标签: python typeerror python-3.3

def guessingTime(answer, username):
    vdfile = open("victorydefeat.txt", "a")
    now = time.asctime(time.localtime(time.time()))
    victory = username + "successfully guess" + answer + "on" + now
    defeat = username + "was unable to guess" + answer + "on" + now
    print("That's 20! Time to guess.")
    guess = input("Is it a(n): ")
    if guess.lower() == answer:
        print("You got it! Congratulations!")
        vdfile.write(victory)
        vdfile.write("\n")
    else:
        print("Sorry, but the answer was", answer)
        vdfile.write(now)
        vdfile.write("\n")
    vdfile.close()

def main():
    print("Welcome to 20 questions! The game where I (your humble program) will think of an object and you (the beloved user) will try to guess it!")
    username = print(input(("Now, before we begin, I'd like to know your name (for recording purposes): ")))
    infile1, infile2, answer = getAnswer()
    #startAsking(infile1, infile2)
    guessingTime(answer, username)

main()

错误消息是"不支持的操作数类型+ =' NoneType'和' str'"为了"胜利="线。我想写:"成功猜到"。我该怎么办?

2 个答案:

答案 0 :(得分:1)

print()不会返回值。因此,username设置为None

input()将打印传递的字符串并返回输入的文本。您只需要以下内容:

username = input("Now, before we begin, I'd like to know your name (for recording purposes): ")

答案 1 :(得分:0)

您可以使用time.asctime(),默认为本地时间。另外,字符串格式是你的朋友:

"{0} guessed {1} on {2}".format(username, answer, time.asctime())

但是你的一个论点usernameanswerNone(正如unholysampler所解释的那样),这就是你得到那个特定错误的原因。