如何在Python中计算用户输入

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

How to count user input in python

问题

I want to get the user to input a list of numbers (1 or 2 digits ex 2,34,4,35,36) and for python to send back how much total numbers there is but the problem is python counts each number as a unit when I want it to know the difference between 2 and 34 for example.

我想让用户输入一个数字列表(1或2位数,例如2,34,4,35,36),然后让Python返回总共有多少个数字,但问题是Python将每个数字都计算为一个单位,而我希望它能区分234之间的差异。

Initially , I tried the len function then I cam here where I saw the count function but even then it still doesn't work

最初,我尝试了len函数,然后我来到这里看到了count函数,但即使那样它仍然不起作用

count(i)```

(`count`甚至没有被识别?出现某种原因)

I decided to go back to `len` and I did this

我决定回到`len`,然后我做了这个

```i = input ("enter a list")
print ( len(i) )```

It did work but it counts every single number separately it even counts the commas!!!

它确实起作用,但它将每个数字都分开计算,甚至包括逗号!

I put the list  `34,34,34,35` and I got `11` and I wanted to get `4` because that's how many numbers there are

我输入了列表`34,34,34,35`,结果得到了`11`,但我希望得到`4`,因为那里有4个数字

Thank you for taking the time to read my ranting

谢谢您抽出时间阅读我的抱怨

<details>
<summary>英文:</summary>

So I want to get the user to input a list of numbers `(1 or 2 digits ex 2,34,4,35,36)` and for python to send back how much total numbers there is  but the problem is python counts each number as a unit when I want it to know the difference between `2` and `34` for example. Idk my brain isn&#39;t braining.

Initially , I tried the `len` function then I cam here where I saw the count function but even then it still doesn&#39;t work

i = input("enter a list")
count(i)


(count isnt even recognized? for some reason)

I decided to go back to `len` and I did this

i = input ("enter a list")
print ( len(i) )


It did work but it counts every single number separately it even counts the commas!!!

I put the list  `34,34,34,35` and I got `11` and I wanted to get `4` because that how much numbers there is

Thank you for talking the time to read my ranting

</details>


# 答案1
**得分**: 1

你最好使用空格而不是逗号。这样,你可以使用内置的 Python `.split()` 方法,它会将字符串拆分为列表。

例如,以下代码的输出如下。 (虽然你的代码使用了输入,但由于输入本质上也是字符串,我只是创建了一个具有字符串值的变量,因为这与输入的变量类型相同。)
```python
text = "34 35 36 37"
text = text.split()
print(text)
print(len(text))

这将产生以下输出:

['34', '35', '36', '37']
4
英文:

Your best bet is to do spaces, instead of commas. This way, you can use the built in python .split() method which will split a string into a list.

For example, the following code has the following output. (Yours uses a input though, but since inputs are strings anyways, I just created a variable with a string since that is same variable type as inputs.

text = &quot;34 35 36 37&quot;
text = text.split()
print(text)
print(len(text))

That produces the output of:

[&#39;34&#39;, &#39;35&#39;, &#39;36&#39;, &#39;37&#39;]
4

答案2

得分: 0

你可以执行 i = (input("输入一个列表").count(",")) + 1

这会计算逗号的数量,并加一,因为最后一个数字不会有逗号。

就像上面的评论者所说,如果你想要一个以逗号分隔的列表,你可以执行

i = input("输入一个列表").split(',')
print(len(i))
英文:

You could do i = (input(&quot;enter a list&quot;).count(&quot;,&quot;)) + 1

This counts the number of commas, and adds one since the last number will not have a comma.

Like the commenter said above, if you want a list of it separated by commas, you can do

i = input(&quot;enter a list&quot;).split(&#39;,&#39;)
print(len(i))

答案3

得分: 0

The variable i is a string, therefore when you use len() it will count all the characters. To count the numbers separated by commas you can use the split function.

i = input("enter a list") # 34,34,34,35
print(len(i.split(",")))

英文:

The variable i is a string, therefore when you use len() it will count all the characters. To count the numbers separated by commas you can use the split function.

 i = input(&quot;enter a list&quot;)  #  34,34,34,35
 print(len(i.split(&quot;,&quot;)))

huangapple
  • 本文由 发表于 2023年5月11日 01:12:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221026.html
匿名

发表评论

匿名网友

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

确定