gpt4 book ai didi

python - 将其写入文件的正确语法是什么?

转载 作者:太空宇宙 更新时间:2023-11-03 18:30:49 27 4
gpt4 key购买 nike

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()

“victory =”行的错误消息是“+= 'NoneType' 和 'str' 不支持的操作数类型”。我想写:“成功猜中”。我该怎么办?

最佳答案

print() 不返回值。因此用户名被设置为None

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

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

关于python - 将其写入文件的正确语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22412047/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com