【问题标题】:module thats not producing output不产生输出的模块
【发布时间】:2021-11-15 12:13:03
【问题描述】:

这里是主要的驱动代码。

import Proj05Runner

str = 'The lazy brown fox jumped over the fence.'
subStr = 'fox'
result = Proj05Runner.run(str,subStr)
print(result)

这是我目前拥有的需要与驱动程序一起使用的模块的代码。

def run(str, subStr):
"""Returns a substring of any given string based upon a given range of characters before and after the substring"""

str_range = 3

if subStr in str:
    str_modified = str[str.index(subStr) - str_range : str.index(subStr) + len(subStr) + str_range]
    return ("I certify that this program is my own work " + "\n" + str + "\n" + subStr + "\n" + str_modified +
            "and is not the work of others. I agree not " + "\n" + str + "\n" + subStr + "\n" + str_modified +
            "to share my solution with others. " + "\n" + str + "\n" + subStr + "\n" + str_modified +
            "Print your name here." + "\n" + str + "\n" + subStr + "\n" + str_modified)
else:
    return "ERROR: subStr not found in str!"

例如,输出没有正确生成

I certify that this program is my own work 
The lazy brown fox jumped over the fence.
fox
wn fox juand is not the work of others. I agree not 
The lazy brown fox jumped over the fence.
fox
wn fox juto share my solution with others. 
The lazy brown fox jumped over the fence.
fox
wn fox juPrint your name here.
The lazy brown fox jumped over the fence.
fox
wn fox ju

这是我需要的输出

I certify that this program is my own work
and is not the work of others. I agree not
to share my solution with others.
Print your name here.

The lazy brown fox jumped over the fence.
fox
wn fox ju

im sorry guys im new to this..

【问题讨论】:

  • 这不是让字符串跨越多行代码的正确方法。
  • 你的代码的问题是你不能在run(或任何地方)内调用str()
  • 较小的问题是代码没有正确缩进
  • 我现在有点让它运行,但它没有正确产生输出

标签: python python-3.x python-2.7 python-requests scripting


【解决方案1】:

这是你需要的吗:

def run(str, subStr):
    """Returns a substring of any given string based upon a given range of characters before and after the substring"""

    str_range = 3
    
    if subStr in str:
        str_modified = str[str.index(subStr) - str_range : str.index(subStr) + len(subStr) + str_range]
        return ("I certify that this program is my own work \nand is not the work of others. I agree not \nto share my solution with others.\nPrint your name here.\n\n" + str + "\n" + subStr + "\n" + str_modified)
    else:
        return "ERROR: subStr not found in str!"

str = 'The lazy brown fox jumped over the fence.'
subStr = 'fox'
out = run(str, subStr)
print (out)

输出:

I certify that this program is my own work 
and is not the work of others. I agree not 
to share my solution with others.
Print your name here.

The lazy brown fox jumped over the fence.
fox
wn fox ju

【讨论】:

    猜你喜欢
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    • 2012-01-07
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    • 2013-10-06
    相关资源
    最近更新 更多