Typeerror: can only concatenate str (not "_io.Textiowrapper") to str

Asked

Viewed 28 times

0

Hello, I’m trying to make a Virtual Assisstente, but it’s giving me an error that I don’t realize what’s going on

name = open("user_data.txt")
user = name.read()

def main():
    main_question = input("O que deseja, " + name + "?")

And makes the following mistake:

    main_question = input("O que deseja, " + name + "?")
TypeError: can only concatenate str (not "_io.TextIOWrapper") to str

1 answer

1


This error is occurring because you are passing the variable name that opens the file for reading. You should use the variable user because you’ve already made the call of the method .read() on it that returns the contents of the file.

   main_question = input("O que deseja, " + user + "?")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.