不能在一行中写三个值。

huangapple go评论75阅读模式
英文:

Can't not write three value in 1 line

问题

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 integer in 1 line)
    It shows me an error:
File "c:\Users\Administrator\Documents\Code\Python\baitap(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 identifies 'a' but not 'b', 'c', so can you show me how to fix it or are there other ways to write it in 1 line?

英文:

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 &quot;c:\Users\Administrator\Documents\Code\Python\baitap(LQDOJ)\EZMIN.py&quot;, line 1, in &lt;module&gt;
    a = int(input())
ValueError: invalid literal for int() with base 10: &#39;2 5 6&#39; 

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?

答案1

得分: 1

你遇到的错误是因为你试图将一个包含多个数字的字符串转换为整数。int() 函数只能将单个数字转换为整数。

要解决这个问题,你可以使用split()函数将输入字符串拆分为字符串列表,然后使用map()函数将列表中的每个字符串转换为整数。

a, b, c = map(int, input().split())
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(min_value)
英文:

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 &lt; min:
min = y
if z &lt; min:
min = z
print(min)

答案2

得分: 0

Python 的输入不是这样工作的。当你使用 input() 时,你是在告诉计算机从命令行接受输入,直到用户按下回车键为止。输入不是用空格分隔的。你之所以会收到这个错误,是因为程序无法从输入 2 5 6 中解析出一个整数。据我所知,没有一种方式可以将所有的输入放在同一行上。

正确的输入应该是这样的:

2
5
6

在按下回车键以创建新行时。

英文:

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.

答案3

得分: 0

方法 1

你遇到的错误是因为你尝试使用 int() 函数将整个字符串 '2 5 6' 转换为整数。然而,int() 函数期望一个单一的整数值,而不是一个包含多个数字的字符串。
代码:

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("最小值是:", min_value)

你将会被提示分别输入 a、b 和 c 的值,代码将正确计算并显示这三个和中的最小值。

方法 2

使用这个方法更加优化:

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("最小值是:", min_value)
  • split() 方法在空格处拆分输入字符串,创建一个包含字符串元素的列表。
  • map() 函数将 int() 函数应用于拆分列表的每个元素,将它们转换为整数。
  • list() 用于将生成的 map 对象转换为整数列表。
    生成的列表存储在 input_list 中,用于进一步计算。
英文:

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 &lt; min_value:
    min_value = y
if z &lt; min_value:
    min_value = z

print(&quot;The minimum value is:&quot;, 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(&quot;The minimum value is:&quot;, 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.

huangapple
  • 本文由 发表于 2023年6月1日 12:04:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定