Detecting the max value from an array and printing it as text instead of value

Question:

Hello all, I have been self-learning python for the past week using some audiovisual content and I am trying to solve advent of code, the problem requires to input some values for several elfs, add those values individually and check the one with the highest number.
I manage to get to the point that I can determine which one has the highest load my doubt is how can i add that to any new variable and print it with losing the one with the highest number.
This is the code I got so far

`#input number of items caried by each elf and how many calories each item has
elf_1 = input("insert item calories of elf 1(separated by space):")
elf_2 = input("insert item calories of elf 2(separated by space):")
elf_3 = input("insert item calories of elf 3(separated by space):")
elf_4 = input("insert item calories of elf 4(separated by space):")
elf_5 = input("insert item calories of elf 5(separated by space):")

#transform string values into intengers
    #Elf 1
variable_1 = elf_1.split()
str_to_int_1 = list(map(int, variable_1))
    #Elf 2
variable_2 = elf_2.split()
str_to_int_2 = list(map(int, variable_2))
    #Elf 3
variable_3 = elf_3.split()
str_to_int_3 = list(map(int, variable_3))
    #Elf 4
variable_4 = elf_4.split()
str_to_int_4 = list(map(int, variable_4))
    #Elf 5
variable_5 = elf_5.split()
str_to_int_5 = list(map(int, variable_5))

#obtaining the total number of calories carried by each elf
total_1 = sum(str_to_int_1)
total_2 = sum(str_to_int_2)
total_3 = sum(str_to_int_3)
total_4 = sum(str_to_int_4)
total_5 = sum(str_to_int_5)

#checking the elf with the highest number of calories
calorie_table = (total_1, total_2, total_3, total_4, total_5)
highest_calorie = max(calorie_table)
print("The elf with the highest calories is: " + highest_calorie)`

for example, imagine total 3 has the highest amount of calories, how can I print something like "The elf with the highest calories is: elf no3" instead of the calorie ammount.

I tried transforming the total values to text variables but I lose track of the one who has the highest amount of calories.

Thank you for your support

Asked By: Gonçalo Rocha

||

Answers:

It’s a very unconvenient approach to repeat code to do the same things 5 times. Whenever you deal with such repetition, it’s better to use data structures like list or dict, and process it with for loops, while loops and functions. For example, your code can be shortened to this:

num_of_elves = 5
max_calories = 0
elf_with_max_calories = -1
for i in range(num_of_elves):
  elf_items = input(f"insert item calories of elf {i+1}(separated by space):")
  elf_items = elf_items.split()
  elf_items = list(map(int, elf_items))
  elf_calories = sum(elf_items)
  if elf_calories > max_calories:
    max_calories = elf_calories
    elf_with_max_calories = i+1

print("The elf with the highest calories is: ", max_calories)
print("For elf #", elf_with_max_calories)

EDIT:

Regarding the comment, if you want to read the number of elves from the console and check that the values is valid (in your case a positive integer), you can check first if the input values contains only numbers, after that we will know that it can be converted to an integer using int function. Then we can check if the value is positive:

  string = input("How many elves?: ")
  # if string contains only digits, and is a positive integer
  if string.isdigit() and int(string) > 0: 
    num_of_elves = int(string)
  else:
    print("You didn't enter a valid number (positive integer)")
    quit()

It would be more convenient if you kept asking for the value until the user inputs a correct value, for this you can use an ininite loop. It will break when the value is correct, and then the rest of the program will continue

while True: # repeat infinitely
  string = input("How many elves?: ")
  # if string contains only digits, and is a positive integer
  if string.isdigit() and int(string) > 0: 
    num_of_elves = int(string)
    break # stop repeating when input is correct
  else: # not a valid number
    print("You didn't enter a valid number (positive integer)")
Answered By: Alexey Larionov
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.