Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I have this program that calculates the time taken to answer a specific question and quits out of the while loop when the answer is incorrect, but i want to delete the last calculation, so I can call min() and it not be the wrong time, sorry if this is confusing.

from time import time 

q = input('What do you want to type? ') 

a = ' ' 

record = [] 

while a != '': 

start = time() 

a = input('Type: ') 

end = time() 

v = end-start 

record.append(v) 

if a == q: 

print('Time taken to type name: {:.2f}'.format(v)) else: 

break 

for i in record: 

print('{:.2f} seconds.'.format(i))

1 Answer

0 votes
by (106k points)

You can use the slicing notation to keep everything except the last item:

record = record[:-1]

But a better way is to delete the item directly:

del record[-1]

Related questions

+3 votes
2 answers
+2 votes
3 answers
0 votes
1 answer
0 votes
1 answer
asked Oct 8, 2019 in Python by Sammy (47.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...