【问题标题】:Why is my python code giving wrong answers for only some inputs?为什么我的 python 代码只对某些输入给出错误的答案?
【发布时间】:2021-12-25 23:58:33
【问题描述】:

我是 python 新手,我正在尝试编写一个程序,从特定点给出字符串中每个字母的频率。现在我的代码为某些输入提供正确的输出,例如如果我输入“hheelloo”,我得到正确的输出,但如果输入是“hheelllloooo”,则 h、e 和 l 的频率打印正确,但频率如果起点是索引 0,'o' 的值会显示为 7。有人可以告诉我我做错了什么。

编写一个 Python 程序,计算字符串中字符的出现次数(执行 不使用内置计数功能。 修改上面的程序,让它开始计数 从指定位置。

str = list(map(str, input("Enter the string : ")))
count = 1
c = int(input("Enter the location from which the count needs to start : "))
for i in range(c, len(str)):
    for j in range(i+1,len(str)):
        if str[i] == str[j]:
            count += 1
            str[j] = 0
    if str[i] != 0:
        print(str[i], " appears ", count, " times")
        count = 1

【问题讨论】:

  • 尝试在最里面的if 中添加print 语句,例如print(f'str[{i}] was equal to str[{j}], so we will increment the count'),然后使用更简单的'xxx' 输入来测试代码。你看到问题了吗?
  • 你真的有两个任务,对吧?找出字符串中有哪些字符(从起点开始),然后计算出每个字符出现了多少次。正确的?那么,如果您尝试分别解决这些任务怎么办?你能找到更简单的方法来解决这个问题吗?
  • @KarlKnechtel 是的,理解这个问题。

标签: python python-3.x frequency


【解决方案1】:
str = list(map(str, input("Enter the string : ")))
count = 1
c = int(input("Enter the location from which the count needs to start : "))
for i in range(c, len(str)):
    for j in range(i+1,len(str)):
        if str[i] == str[j]:
            count += 1
            str[j] = 0
    if str[i] != 0:
        print(str[i], " appears ", count, " times")
    count = 1 // <----- This line should be outside the if block.

错误是因为缩进。 我刚刚正确缩进了最后一行。

谢谢,如果有效,请点赞。

【讨论】:

  • 感谢兄弟的帮助,它解决了问题。 stackoverflow 不允许我投票,因为我是新手,需要 15 个声誉,但一旦我拥有它们,我一定会投票给你。
  • 好的,萨格尼克。不用担心。
【解决方案2】:

string 模块可以方便地计算字符串中字母、数字和特殊字符的频率。

import string

a='aahdhdhhhh2236665111...//// '

for i in string.printable:
    z=0
    for j in a:
        if i==j:
            z+=1
    if z!=0:
        print(f'{i} occurs in the string a {z} times')

如果你想从一个特定的索引值计算字符出现的频率,你只需将上面的代码稍微修改如下:

import string

a='aahdhdhhhh2236665111...//// '
c = int(input("Enter the location from which the count needs to start : "))
for i in string.printable:
    z=0
    for j in a[c:]:
        if i==j:
            z+=1
    if z!=0:
        print(f'{i} occurs in the string a {z} times')

【讨论】:

    【解决方案3】:

    我认为您不需要将 inputstrmaplist 作为 input() 始终返回 stringstring 本身就是一个 list 的字符。还要确保不要使用内置函数作为变量名(如代码中使用的 str)。一种更简单的方法可以是:

    input_word = input("Enter the string : ")
    c = int(input("Enter the location from which the count needs to start : "))
    # Dict to maintain the count of letters
    counts = {}
    for i in range(c, len(input_word)):
        # Increment 1 to the letter count
        counts[input_word[i]] = counts.get(input_word[i], 0)+1
        
    for letter, freq in counts.items():
        print (f'{letter} appears {freq} times')
    

    输出:

    Enter the string : hheelllloooo
    Enter the location from which the count needs to start : 2
    e appears 2 times
    l appears 4 times
    o appears 4 times
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多