Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何调试我的Python代码?_Python - Fatal编程技术网

如何调试我的Python代码?

如何调试我的Python代码?,python,Python,我的代码的目的是从用户那里获取字符串,并将其转换为不区分大小写的列表。然后我需要从用户那里获取第二个字符串,然后输出第二个给定字符串的位置。这是我的代码: UserSentence = input('Enter your chosen sentence: ') #this is where the user inputs their sentence from string import punctuation #this 'fetches' the punctuation from the s

我的代码的目的是从用户那里获取字符串,并将其转换为不区分大小写的列表。然后我需要从用户那里获取第二个字符串,然后输出第二个给定字符串的位置。这是我的代码:

UserSentence = input('Enter your chosen sentence: ') #this is where the user inputs their sentence
from string import punctuation #this 'fetches' the punctuation from the string
tbl=str.maketrans({ord(ch):" " for ch in punctuation})
UserSentence = UserSentence.lower().translate(tbl).split()#.split() turns the input sentence into a list,...
#...this will help to identify where a word appears...
#...in the sentence. The .lower() also turns the...
#...string into lowercase so it is not case sensitive.
UserWord = input('Enter a word from the sentence: ')#this is where the user inputs their word from the sentence
UserWord = UserWord.lower()#.lower() is used to make UserWord not case sensitive
for i in range(len(UserSentence)):
     if UserSentence (i) == UserWord:
         print ('Your chosen word appears in: ')

要为序列编制索引,您需要使用
[]

if UserSentence[i] == UserWord:
如果你想找到他们的单词是哪个索引,你可以这样做

if UserWord in UserSentence:
    print('Your word is located at {}'.format(UserSentence.index(UserWord)))
或者类似地

try:
    print('Your word is located at {}'.format(UserSentence.index(UserWord)))
except ValueError:
    print('Your word is not in the sentence')
这里有几个错误:

  • 如果您使用的是Python 2,请对字符串使用
    raw\u input
    。在Python3中,输入是可以的
  • 你的
    maketrans
    呼叫很奇怪
  • 如果一个单词在一个列表中,你不需要任何循环或自己的比较。Python可以为您做到这一点
  • 请坚持。它告诉您应该如何格式化代码,以便更易于阅读

  • 您的重写和测试代码:

    from string import punctuation, maketrans
    
    user_sentence = raw_input('Enter a sentence: ')
    trans = maketrans("", "")
    user_sentence = user_sentence.lower().translate(trans, punctuation).split()
    user_word = raw_input('Enter a word from the sentence: ')
    user_word = user_word.lower()
    if user_word in user_sentence:
        print ('Your chosen word appears in the sentence.')
    

    “需要调试我的代码的帮助吗”-告诉我们哪里出了问题。投票结束时没有MCVE。“使用字符串的原始输入。”从Python 3.x开始,这在2.x中才是正确的