Yes ,I have run the code I got the output as well printed on the text file I didn’t get any error message …but unable to complete the hands on … may be the handson questions is expecting something different … thats why I provide the link to the question as well … Will you be able to check and validate please?
I don’t expect that error if entered correctly. Perhaps you are copy/pasting entire code and running into problems. Try running the code after typing in each line separately. You’ll start to see where the errors are coming from.
Thnaks much pylang . I was able to fix the previous error with your guidance , much appreciated
can you guide me with below
###Start code here
r_squared = 0.484
###End code(approx 1 line)
with open(“output.txt”, “w”) as text_file:
text_file.write("rsquared= ", r_squared)
______--------- I am getting a typeerror
TypeError Traceback (most recent call last)
in
3 ###End code(approx 1 line)
4 with open(“output.txt”, “w”) as text_file:
----> 5 text_file.write("rsquared= ", r_squared)
TypeError: write() takes exactly one argument (2 given)
Are you getting an error from the “import statsmodel.api as sm” first?
Otherwise, I cannot see any possible way you would get that NameError
from the code you show. Not unless there is extra code that you haven’t
shown us.
Writing to a file is not like print. With print you can give any number
of arguments, and it will automatically convert each one to a string and
print them all on one line:
print("rsquared= ", r_squared)
will print “rsquared= 0.484”. But for writing to a file, you have to
generate a single string. Here are some ways to do it:
# C-style % string interpolation
s = "rsquared= %s" % r_squared
# Like above but control the number of decimal places
s = "rsquared= %.8f" % r_squared # 8 decimal places
# Format method:
s = "rsquared= {}".format(r_squared)
# F-string
s = f"rsquared= {r_squared}"
then write to the file:
text_file.write(s + "\n")
The “\n” concatenates an end-of-line character (newline) after the
string. If you are on Windows, Python will take care to convert that to
a CR+NL pair automatically.