英文:
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将每个数字都计算为一个单位,而我希望它能区分2
和34
之间的差异。
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't braining.
Initially , I tried the `len` function then I cam here where I saw the count function but even then it still doesn'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 = "34 35 36 37"
text = text.split()
print(text)
print(len(text))
That produces the output of:
['34', '35', '36', '37']
4
答案2
得分: 0
你可以执行 i = (input("输入一个列表").count(",")) + 1
这会计算逗号的数量,并加一,因为最后一个数字不会有逗号。
就像上面的评论者所说,如果你想要一个以逗号分隔的列表,你可以执行
i = input("输入一个列表").split(',')
print(len(i))
英文:
You could do i = (input("enter a list").count(",")) + 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("enter a list").split(',')
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("enter a list") # 34,34,34,35
print(len(i.split(",")))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论