Can't not write three value in 1 line

Question:

I was having this problem about last week in this code

a = int(input())
b = int(input())
c = int(input())
print(min(a+b,b+c,c+a))
  • so when I enter three input like this: 2 5 6 (three interger in 1 line)
    It show me a error:
File "c:UsersAdministratorDocumentsCodePythonbaitap(LQDOJ)EZMIN.py", line 1, in <module>
    a = int(input())
ValueError: invalid literal for int() with base 10: '2 5 6' 

and I see that it only identify ‘a’ but not identify ‘b’ , ‘c’ so can you show me how to fix it or are there other ways to write it in 1 line?

Asked By: MASAKADEV

||

Answers:

Python inputs to not work this way. When you use input(), you are telling the computer to take input from the command line until the user presses enter. Inputs are not separated by spaces. You are getting this error because the program can not parse an integer from the input 2 5 6. AFAIK, there is not a way to put all of the inputs in one line.

A correct input would be this:

2
5
6

While pressing enter to make new lines.

Answered By: MrDiamond

The error you are getting is because you are trying to convert a string that contains multiple numbers to an integer. The int() function can only convert a single number to an integer.

To fix this, you can use the split() function to split the input string into a list of strings, and then use the map() function to convert each string in the list to an integer.

a, b, c = map(int, input().split())
x = a + b
y = b + c
z = c + a
min = x
if y < min:
min = y
if z < min:
min = z
print(min)
Answered By: dheepss

Method 1

The error you’re encountering is because you’re trying to convert the entire string ‘2 5 6’ into an integer using the int() function. However, the int() function expects a single integer value, not a string containing multiple numbers.
code:

a = int(input())
b = int(input())
c = int(input())

x = a + b
y = b + c
z = c + a

min_value = x
if y < min_value:
    min_value = y
if z < min_value:
    min_value = z

print("The minimum value is:", min_value)

you’ll be prompted to enter the values for a, b, and c separately, and the code will correctly calculate and display the minimum value among the three sums.

Method 2

Using This one is more optimize solution

input_values = input()
input_list = list(map(int, input_values.split()))

min_value = min(input_list[0] + input_list[1], input_list[1] + input_list[2], input_list[2] + input_list[0])

print("The minimum value is:", min_value)
  • The split() method splits the input string at spaces, creating a list of string elements.
  • The map() function applies the int() function to each element of the split list, converting them into integers.
  • list() is used to convert the resulting map object into a list of integers.
    The resulting list is stored in input_list for further calculations.
Answered By: Bothraj P
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.