Reputation: 1
Very beginner question here! I'm trying to create a program that asks for 5 game results, takes away the best and the worst one and counts the average on the ones that are left -> final result.
I think I got the math and the lists right, BUT I can't find a way to get a running number inside of the input. (Example given in the #code comments.) I tried to look for a way to get this, but couldn't. Python says "TypeError: input expected at most 1 argument, got 2". I understand it means that I can't put the serial number inside the input then?
Is there another way to create this? I tried to go around it with function-type solution already, but could not figure it out.
def main():
round = 1
performance_results = []
while round < 6:
time = float(input("Enter the time for performance: ", round))
# "enter the time for performance 1:
# "enter the time for performance 2:
# "enter the time for performance 3: ... till the end of while
performance_results.append(time)
round += 1
# max and min from the list
max_result = (max(performance_results))
min_result = (min(performance_results))
# removes max and min from the list
performance_results.remove(max_result)
performance_results.remove(min_result)
# sum from all the numbers left on the list
sum = performance_results[0]+performance_results[1]+performance_results[2]
# average from the results
final_result = sum / 3
print("The official competition score is", final_result,"seconds.")
if __name__ == "__main__":
main()
Upvotes: -1
Views: 50
Reputation: 5531
This should help u:
float(input(f"Enter the time for performance {round}:"))
And as Amit mentioned, don't use keywords such as round
as var names.
Upvotes: 0
Reputation: 26
If im interpreting this question correctly; you want to change the input it asks depending on the number, we can do this using string formatting!
Python has a few ways we can format strings (depending on your python version you may be limited to fewer)
f-string formatting
f strings are the equivelent of js's string litterals providing a very easy to read format and are notated by the f
at the start of the string, note: for python 3.6+ iirc
a = 123
b = "hello"
my_msg = f"{b} world, have some numbers: {a}"
print(my_msg)
# will give us as a response:
>>> hello world, have some numbers: 123
format() method
str.format()
is a less readable than f strings but can be alot more useful in places.
they work for Python 3.x+ and use {}
place holders
a = 123
b = hello
my_msg = "{} world, have some numbers: {a}".format(b, a=a) # yes! you can use place holders to be used as kwargs.
print(my_msg)
# will give us as a response:
>>> hello world, have some numbers: 123
There are other methods of formatting however these are the most popular and people have already mentioned another example.
Upvotes: 0
Reputation: 3346
you dont need to pass round as parameter to input function instead do the string formatting here.
try below code:
time = float(input("Enter the time for performance %s:" % round))
This will put value of round inside the string
TIP: round is python keyword you can use any other variable name instead of round
Upvotes: 1