I'm writing a program that loads a list of data from a file and I need the program to differentiate if the data in the line is either a string or an integer. However in the code I have done the program doesn't differentiate numbers from strings.
An example of the list of data I have:
HAJOS
ALFRED
1896
1
My code:
def medalsYear():
  times = 1
  totalGold = 0
  totalSilver = 0
  totalBronze = 0
  while times <= 5:
    alpha = fob.readline() #reads file line by line#
    print(alpha)
    times = times + 1
    if type(alpha) == int:
        if alpha == 1:
            totalGold = totalGold + 1
            print("gold medal won")
        elif alpha == 2:
            totalSilver = totalSilver + 1
            print("silver medal won")
        elif alpha == 3:
            totalBronze = totalBronze + 1
            print("bronze medal won")
        else:
            pass
    else:
        print('is a string')
  print(totalGold, "Gold medals won")
  print(totalSilver, "Silver medals won")
  print(totalBronze, "Bronze medals won")
My problem is that when the program reads a line that has an integer, it doesn't determine correctly if the line contains an integer and from there run through the corresponding if statement. Currently my output looks like this.
HAJOS
is a string
ALFRED
is a string
1896
is a string
1
is a string
is a string
0 Gold medals won
0 Silver medals won
0 Bronze medals won
done

int(alpha)will convertalphato a integer or throw aValueErrorif it can't.if alpha.isdigit()will also work