Here's the code I wrote to put the names and numbers into a file:
def main():
num_players = int(input("How many players are there?"))
golf = open("golf.txt", 'w')
for i in range(num_players):
player_name = str(input("What is the player's name?"))
player_score = int(input("What is their golf score?"))
golf.write(player_name + '\n')
golf.write(str(player_score) + '\n')
golf.close()
main()
and the output:
Palmer
120
Nicholas
118
Dalton
150
Woods
122
Player
124
In the following code, I am trying to compare the scores to see whose golf score is the best and the worst. I keep getting an error because str
object cannot be interpreted as an integer. How can I fix this?
def main():
golf = open("golf.txt",'r')
large = 125
small = 119
name=golf.readline()
while name != '':
for i in range(name):
name = golf.readline()
score = int(golf.readline())
if score > large:
large = score
print(name,"is the worst at golf")
while name != '':
for i in range(name):
name = golf.readline()
score = int(golf.readline())
if score < small:
large = score
print(name, "is the best at golf")
main()