Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I can not get the write method to work. #!/usr/bin/python import sys #FUNCTION g

ID: 3757168 • Letter: I

Question

I can not get the write method to work. #!/usr/bin/python import sys #FUNCTION gather user input def main(): firstname = input("Enter your first name. ").strip() lastname = input("Enter your last name. ").strip() handicap = input("Enter your handicap. ").strip() score = input("Enter your golf score. ").strip() i = 1 n = 2 while i<= n: i += 1 print(i) main() #OPEN file file = open("golffile.txt","w") # HEADINGS file.write(" Firstname" + " " + " Last name" + " " + "Handicap" + " " + "Score" + " " + " ") #WRITE firstname lastname handicap golf score file.write(firstname + " " + lastname + " " + handicap + " " + score) file.close() f = open("golffile.txt", "r") print(f.read()) f.close() #output should look like this: #First name Last name Handicap Score # Bill Boswell 12 72 # John Doe 19 78

Explanation / Answer

#!/usr/bin/python import sys # FUNCTION gather user input def get_user_input(): firstname = input("Enter your first name. ").strip() lastname = input("Enter your last name. ").strip() handicap = input("Enter your handicap. ").strip() score = input("Enter your golf score. ").strip() return firstname, lastname, handicap, score def main(): i = 0 # OPEN file file = open("golffile.txt", "w") # HEADINGS file.write(" Firstname" + " " + " Last name" + " " + "Handicap" + " " + "Score" + " " + " ") while i < 2: firstname, lastname, handicap, score = get_user_input() # WRITE firstname lastname handicap golf score file.write(firstname + " " + lastname + " " + handicap + " " + score + " ") i += 1 file.close() f = open("golffile.txt", "r") for line in f: print(line) f.close() # output should look like this: #First name Last name Handicap Score # Bill Boswell 12 72 # John Doe 19 78 main()
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote