编写一个名为longest的函数,它将采用一串空格分隔的单词并返回最长的单词。例如:最长(“这是andela”)=> " andela"最长(“a”)=> " A" [英] Write a function called longest which will take a string of space-separated words and will return the longest one. For example: longest("this is andela") => "andela" longest("a") => "a"

查看:152
本文介绍了编写一个名为longest的函数,它将采用一串空格分隔的单词并返回最长的单词。例如:最长(“这是andela”)=> " andela"最长(“a”)=> " A"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在如何根据需要搜索字符串时失败了



我尝试过的事情:



 def长度(列表):
a = 0
answer =''
列表中的项目:
x = len (项目)
如果x> a:
a = x
answer = item
elif x == a:
如果项目不在列表中:
answer = answer +''+ item
返回答案
打印长度(列表)

解决方案

我无法测试该代码,但我会将其更改为类似的东西。

  def 长度(列表):
a = 0
answer = ' '
项目 列表:
x = len(item)
if x> a:
a = x
answer = item
elif x == a:
如果项目 列表:
answer = answer + < span class =code-string>' ' + item
return 回答
打印长度(列表)





当你不理解你的代码在做什么或为什么它做它的作用时,答案是调试器

使用调试器来查看代码在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



27.3。 pdb - Python调试器 - Python 3.6.1文档 [ ^ ]

使用Python进行调试Python征服宇宙 [ ^ ]

pdb - 交互式调试器 - 本周的Python模块 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有找到错误,它只是帮助你至。当代码没有达到预期效果时,你就接近了一个错误。


 def最长(单词):
如果类型(单词) != str:
引发TypeError(输入不是字符串)
my = words.split()

你的=无
first_time = True
for i in my:
val = i

如果first_time或val>你的:
你的= val

返回(你的)


I have failed on how to search through the String as required

What I have tried:

def length(lists):
    a = 0 
    answer = ''
    for item in lists:
        x = len(item) 
    if x > a:
        a = x
        answer = item
    elif x == a:
        if item not in list:
            answer = answer + ' ' + item
    return answer
print length(list)

解决方案

I can't test that code, but I would change it to something like that.

def length(lists):
a = 0
answer = ''
for item in lists:
    x = len(item)
    if x > a:
        a = x
        answer = item
    elif x == a:
        if item not in list:
            answer = answer + ' ' + item
return answer
print length(list)



When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


def longest(words):
  if type (words)!=str:
      raise TypeError ("input not a string")
  my= words.split()
  
  yours= None
  first_time =True
  for i in my:
    val= i
    
    if first_time or val>yours:
      yours=val
      
  return (yours)    


这篇关于编写一个名为longest的函数,它将采用一串空格分隔的单词并返回最长的单词。例如:最长(“这是andela”)=&gt; &QUOT; andela&QUOT;最长(“a”)=&gt; &QUOT; A&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆