【问题标题】:How to count the amount of digits in an input? [duplicate]如何计算输入中的位数? [复制]
【发布时间】:2017-09-03 02:11:56
【问题描述】:

我想计算输入中的数字数量,例如在四位数字 4256 中,我如何找出有多少个四、二、五和六。我会寻找随机数中的数字数量。 编辑: 我继续努力,这就是我得到的。很抱歉问了一个重复的问题。 随机导入

import random

guess = random.randint(1000,9999)
guess = str(guess)
correct = 0

print(guess) #this was only here for testing

number = input("Enter ")
list(number)

if number[0] in guess:
    correct = correct + 1
    print (correct)

【问题讨论】:

  • 有几种不同的方法可以满足您的要求。您能否展示一下尝试过的方法?
  • 正在寻找副本...
  • collections.Counter(input())?

标签: python python-3.x


【解决方案1】:

要计算 x 在 y 中出现的次数,请使用

z = y.count(x)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 2014-11-13
    • 2021-03-10
    • 1970-01-01
    • 2010-11-10
    • 2023-04-08
    相关资源
    最近更新 更多