Question: In Python, I need to add an if statement or loop to check a variable for = then write to a file. I would like
In Python, I need to add an if statement or loop to check a variable for <>= then write to a file. I would like to make a function.
See code below:
#!/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 < 3: firstname, lastname, handicap, score = get_user_input() # WRITE firstname lastname handicap golf score file.write(firstname + " " + lastname + " " + handicap + " " + score + " ") i += 1 file.close()
main()
""" if score >80 file.write("Over Par") elif score < 80 file.write(Under Par") elif score = 80 file.write("Made Par")
If the score is = Par, then display 'Made Par' If the score is < Par, then display 'Under Par' If the score is > Par, then display 'Over Par'
"""
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
