Reputation: 173
I am currently writing a program that tells the user about his/her video game usage. The user is asked to enter the number of hours spent gaming for each day of the week. The program then tells them
Here is what my code looks like right now;
while True:
name = input("Hello! Please enter your name: ")
if name.isalpha():
break
else:
print("Please enter a valid name.")
print("Hello {}! Lets get started!".format(name))
week_hours = []
for i in range(7):
while True:
try:
hours = int(input("How many hours did you spend gaming on day
{}".format(i+1)))
week_hours.extend([hours])
break
except ValueError:
print("please enter a valid integer.")
total = sum(week_hours)
print("you spent a total of {} Hours gaming in the past 7 days!".format(total))
average = total/7
print("On average, you spent {} hours gaming per day.".format(average))
(The while loops are for data validation and making sure they can't enter letters/numbers when they are not supposed to.)
I am currently writing a print statement that tells the user the highest amount of hours spent gaming in one day. Here is where I am at;
print("You played video games the most on day {}, spending {} hours!".format())
When asked how many hours the user has spent gaming, the user's input is stored in a list. Once the user has entered 7 values into the list (one for each day of the week,) It breaks out of the loop and continues with the rest of the program.
Now, on to the question.
How would I be able to return the index value of the highest int that is stored in that list, and then put it into the print statement using ".format"?
Upvotes: 0
Views: 1131
Reputation: 6053
Sorry. At It first I misunderstood the question. If you need to get index of the largest number in the list. You can try this:
# hours is your list of values.
hours = [...]
highest_value = max(hours)
index = hours.index(highest_value)
Note that it won't work if you have duplicate maximum values in your list.
If you just need the largest value, then max
is all that you need.
Upvotes: 1