Ce化学元素:化学元素符号任务(chemical element r)

我必须做一个任务,我们需要检查一个符号是否是一个有效的符号(通过任务的规则)

我可能无法解释的任务的背景信息:

作业提供的示例:

def isvalid(symbol, element, length = None):
    elbool = False
    
    if symbol[0]!=symbol[0].upper():
        return False #first letter in the symbol has to be a capital letter
    
        
    for i in range(len(symbol)-1):
            
        if element.find(symbol[i+1])>element.find(symbol[i]): #checking if the order is correct
            elbool = True
        else:
            return False
    
    if length is not None:
        if len(symbol)!=length:
            return False
        else:
            elbool = True
           
        
    return elbool

现在是我的代码,但它不工作,例如这个:isvalid('Rcm','Americium'),因为在 c 之前有一个 m,它计算那个。

所以我想我需要从符号中的最后一个字母拆分元素字符串,所以我没有这个问题,但是我该怎么做呢?

对不起,如果这个问题有点混乱。

1

您需要使用.find(needle, start_pos)element中的某个位置之后查找字符。此外,您不需要弄乱索引并继续从symbol中查找上一个当前字符。只需跟踪下一次迭代的当前字符位置即可。

您还应该进行不区分大小写的搜索,因为,使用您的示例,"Americium"中没有"R"。我通过将element转换为小写一次,然后对symbol中的每个字符执行.find(c.lower(), ...)

最后,您忘了检查symbol中第一个字符以外的所有字符都是小写的。

element = element.lower() # Lowercase element so that we can find characters correctly
lastfound = 0
for ix, c in enumerate(symbol):
    if ix > 0 and c.isupper():
        # Not the first character, and is uppercase
        return False
    thisfound = element.find(c.lower(), lastfound) # Find the location of this character
    if thisfound == -1: # character not found in element after location lastfound
        return False
    lastfound = thisfound # Set lastfound for the next iteration

其他一些小建议:

你可以return False只要你发现有问题,然后,在函数的末尾,只需return True,因为你到达终点的唯一方法是什么都没有错。

您可以使用symbol[0].islower()检查字符是否为小写。无需执行symbol[0] != symbol[0].upper()

在检查字符顺序之前,您应该检查长度要求,因为这是更简单的检查。

应用所有这些:

def isvalid(symbol, element, length = None):
    if symbol[0].islower():
        return False
    if length is not None and len(symbol) != length:
        return False
    
    element = element.lower() # Lowercase element so that we can find characters correctly
    lastfound = 0
    for ix, c in enumerate(symbol):
        if ix > 0 and c.isupper():
            return False
        thisfound = element.find(c.lower(), lastfound) # Find the location of this character
        if thisfound == -1: # character not found in element after location lastfound
            return False
        lastfound = thisfound # Set lastfound for the next iteration
  
    return True

使用您的测试:

>> isvalid('Zer', 'Zeddemorium')
True
>> isvalid('Zer', 'Zeddemorium', 2)
False
>> isvalid('di', 'Zeddemorium')
False
>> isvalid('Rcm', 'Americium')
True

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(357)
Css项目:Codepen:项目-CSS不工作
上一篇
Celery教程:是否有与芹菜相关的问题的修复https://github.com/celery/celery/issues/3
下一篇

相关推荐

  • css折行让文本更加美观

    示例示例CSS折行是指在CSS中使用word-wrap属性来控制文本的折行,它允许长单词或URL地址换行显示,而不会被分割。word-wrap属性可以使用以下值:…

    2023-02-20 08:58:55
    0 85 48
  • layui select 样式:1. 小学2. 初中3. 高中

    layui select 样式是指 layui 的 select 组件,它是一个可以让用户从列表中选择一个选项的组件。它可以用来替代 HTML 中的 select 元素,提供更多的功能和更好的用户体验。…

    2023-01-20 09:50:10
    0 35 11
  • css下划线颜色:Welcome to my website!

    CSS 下划线颜色可以通过使用 text-decoration 属性来改变。可以使用 `text-decoration-color` 属性来指定颜色,或者使用 `border-bottom` 属性来指定底部边框的颜色。…

    2023-01-10 07:41:30
    0 98 81
  • css设置粗体:标题

    使用css设置粗体,可以使用 font-weight 属性:也可以使用关键字:…

    2023-01-11 01:13:03
    0 70 96
  • css3.0参考手册:CSS3新特性介绍

    CSS3.0参考手册是一本专门介绍CSS3.0语法和样式的书籍,其中包含了CSS3.0的新特性、新属性、新选择器、新函数以及新媒体查询等。…

    2023-01-16 03:26:34
    0 78 43
  • css图片上怎么加文字:标题文字

    CSS图片上添加文字的方法有很多种,其中一种是使用CSS的。after伪元素。代码如下:…

    2023-03-13 12:57:53
    0 73 74
  • css图片和文字居中对齐:标题

    示例示例对于单行文字:使用`text-align: center;`来实现文字居中对齐。…

    2023-01-29 03:25:23
    0 67 60
  • css 时间轴一段发展历程

    CSS 时间轴是一种使用 CSS 来创建时间轴的技术。它可以为网站提供一个有趣的方式来展示历史事件或重要时刻,而不需要使用 JavaScript 或 Flash。…

    2023-03-18 00:39:40
    0 88 57

发表评论

登录 后才能评论

评论列表(14条)