0

This is my code:

lowMess=input('A\n')
if 'dice(' in lowMess:
  diceLen = len(lowMess)
  dice = []
  for x in range (5,diceLen):
    if x.isnumeric():
      dice.append(x)
print(dice)
LeopardShark
  • 3,820
  • 2
  • 19
  • 33
abc
  • 11
  • 2
    what's wrong with your code? – drum Feb 10 '22 at 19:11
  • 2
    What is the issue with your code? What is the output of your code and what are you expecting? – ksohan Feb 10 '22 at 19:12
  • 2
    Did you mean `lowMess[x].isnumeric()`? And `dice.append(lowMess[x])`? – trincot Feb 10 '22 at 19:13
  • 3
    x will always be an integer, since the range method returns an integer. – Dr. Casual Feb 10 '22 at 19:14
  • 1
    Welcome to Stack Overflow! Please take the [tour]. You seem to be asking two different questions: the one in the title and "what's wrong with my code?", but the code has at least two issues I can see, so the answer isn't obvious. For the question in the title, see [How to check if string input is a number?](/q/5424716/4518341) For help with debugging your code, you need to make a [mre] including minimal code, example input, expected output, and actual output--or if you get an error, the full error message. You can [edit]. For more tips, see [ask]. – wjandrea Feb 10 '22 at 19:17

1 Answers1

0

Your x is an integer (taken from range(5, diceLen)) and has nothing to do with the input, other than that it could be used as an index in the input string. So you would want to make that access in the input string:

digit = lowMess[x]

Then continue with the check. As noted in comments, for checking whether a character is a digit, isnumeric is not the right tool. Use isdecimal:

if digit.isdecimal():

...and convert to integer:

  dice.append(int(digit))

All this can be done with list comprehension:

dice = [int(digit) for digit in lowMess if digit.isdecimal()]
trincot
  • 317,000
  • 35
  • 244
  • 286
  • It's worth noting that `s.isnumeric()` doesn't mean that `int(s)` will succeed, for example, `s = '⅕'`. For a more thorough solution, see [this answer](/a/5424739/4518341) on "How to check if string input is a number?". – wjandrea Feb 10 '22 at 19:30
  • 1
    @wjandrea, thanks for that correct remark. Updated the answer accordingly. – trincot Feb 10 '22 at 19:41