0

Each number call that contains a value (2,4,8) will output "Clap", if not "No clap"

Call

def clapping (number)

def clapping (772)

output:

"Clap"

I made the program, but it seems something is wrong. Can I ask for help to check which is wrong

import re

def clapping(number):
    return "Clap" if re.findall("[248]+",number) else "No Clap"

print(clapping(779))
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • You don't use `def` when you're calling the function with an argument. – Barmar Jun 12 '20 at 05:54
  • So i don't need to use def in running the program? Is it like that, sir? – Yeni Eldima Jun 12 '20 at 05:55
  • You use `def` when defining a function, not when calling it. you don't write `def clapping(772)`, you just write `clapping(772)` – Barmar Jun 12 '20 at 05:56
  • Sir, I want to ask. Which part in my program that wrong ? – Yeni Eldima Jun 12 '20 at 05:58
  • I posted an answer below. – Barmar Jun 12 '20 at 05:58
  • Sir, is findall function can only read strings? – Yeni Eldima Jun 12 '20 at 05:59
  • Yes, didn't I say that in my answer below? All the regexp functions only work on strings. – Barmar Jun 12 '20 at 06:02
  • Yes sir, I understand now – Yeni Eldima Jun 12 '20 at 06:04
  • Yeni, when deciding which tags to include you need to think how they might be used to filter your question in or out in searches. Might someone want to see questions with a "regex" tag? Yes, definitely. What about "list", "function" and the others? I don't think so. Among questions that were concerned with lists, for example, only a tiny fraction would have that tag, so why search on it? The same for the others. What about readers who want to see Python questions? There are lots of those, but they may miss your question. I suggest you have two tags: "python" and "regex". – Cary Swoveland Jun 12 '20 at 07:08

1 Answers1

2

The regexp functions require a string to search, so you have to convert the number to a string with str(number).

There's also no need to use findall(). You only need to know if the regexp matches one time, you don't need a list of all the matches. Similarly, you don't need the + quantifier, since matching a single character is enough.

def clapping(number):
    return "Clap" if re.search("[248]",str(number)) else "No Clap"
Barmar
  • 741,623
  • 53
  • 500
  • 612