代码之家  ›  专栏  ›  技术社区  ›  Chevady Ju

使用要求为word+任意整数的“if”的问题

  •  -1
  • Chevady Ju  · 技术社区  · 7 年前

    出于某种原因,它一定是以这种形式出现的

    text=input("plz type:")
    constant=30
    if text == "!dollar "+**any integer**
        print(any integer*constant) 
    

    有可能在这种需求中使用“如果”吗?

    并进一步计算得到整数

    谢谢

    1 回复  |  直到 7 年前
        1
  •  3
  •   DeepSpace    7 年前

    您应该使用正则表达式:

    import re
    
    text = input("plz type:")
    constant = 30
    match = re.match(r'!dollar(\d+)', text)
    if match:
        print(int(match.group(1)) * constant)