1

---- -So here is my code-----

print("Type 3 numbers (comma separated)")
x,y,z = int(input()).split(",")
Avg = (x + y + z)/3
print(f"So here is the average {Avg}")

It is about asking the user to input 3 numbers and it calculates the average of them But an invalid literal error occurs later i just putted int() in Avg variable like this

Avg = int((x+y+z))/3
it works but i want to know why my code didn't worked earlier?
1
  • 3
    It should be x,y,z = map(int, input().split(",")) Commented Apr 13, 2020 at 15:38

1 Answer 1

2

The problem is that you try to convert string that can't be converted to int (with commas, returned by input()). Try this one, it can be used to calc average for any amount of numbers you choose:

print("Type numbers (comma separated)")
nums = [int(n) for n in input().split(",")]
Avg = sum(nums)/len(nums)
print(f"So here is the average {Avg}")
Sign up to request clarification or add additional context in comments.

4 Comments

Means that in user input (for eg. 4,5,6) . the commas here will cause an error?
And can you please explain what does this n.strip() means . I did'nt learnt python completely so i want to know
Yes, you can convert "5"(string) to int but when you have commas (or any other non-numeric character), it couldn't be converted to int. strip() removes both leading and trailing spaces from the string but you know what, in this case you don't need it. I've updated my solution...
Thanks mate :) :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.