1

Resolved and Fixed How do I close this? :D

I've been working none stop lately on adding features to my chat bot and occasionally come across an issue that I need assistance in. I've looked at other suggested threads already.

The Short Explanation:

Trying to get it to select a number between 1 and n. The command a user can use would be /roll 20, this returns 1-20. I'm trying to be able to get args to replace random.randint(n) while n = integer. Sorry if my explanation is bad. Been up days working nonstop.

The Original Code(python) before attempt:

  elif used_prefix and cmd.lower() == "roll" and self.rank(user) >= 1:
      args = args.lower()
      if len(args) == 0: post("Select a dice amount(6,8,10,20)")
      else:
        if args == "6": post("Rolled %s" % random.randint(1,6))
        elif args == "8": post("Rolled %s" % random.randint(1,8))
        elif args == "10": post("Rolled %s" % random.randint(1,10))
        elif args == "20": post("Rolled %s" % random.randint(1,20))
        else: post("Select a dice amount(6,8,10,20")

The End Result Code(Python) after attempt:

elif used_prefix and cmd.lower() == "roll" and self.rank(user) >= 1:
  args = args.lower()
  n = args.lower()
  if len(args) == 0: post("Select a dice amount(6,8,10,20)")
  else:
    post("Rolled %s" % str(random.randint(1,n)))

The Traceback

    [STATUS] Setting status Online...
    [LOAD] Loading Errors...
    [LOAD] Loading Locks...
    [SAVE] Appending default...
    Connected 
    Connected 
    Youcoldyet(4)-[devlcars]:[/roll 20](Monday, 11 May 2015, 01:07:23 AM)
    [ERR] Fatal error.
    (C:\Python34\lib\random.py:218) Can't convert 'int' object to str implicitly
    Traceback (most recent call last):
      File "C:\Users\Administrator\Downloads\hmm\bot.py", line 575, in <module>
        TestBot.easy_start(rooms, "Hidden", "Hidden")
      File "C:\Users\Administrator\Downloads\hmm\ck.py", line 1278, in easy_start
        self.main()
      File "C:\Users\Administrator\Downloads\hmm\ck.py", line 1254, in main
        con._feed(data)
      File "C:\Users\Administrator\Downloads\hmm\ck.py", line 550, in _feed
        self._process(food.decode().rstrip("\r\n")) #numnumz ;3
      File "C:\Users\Administrator\Downloads\hmm\ck.py", line 560, in _process
        getattr(self, func)(args)
      File "C:\Users\Administrator\Downloads\hmm\ck.py", line 670, in rcmd_u
        self._callEvent("onMessage", msg.user, msg)
      File "C:\Users\Administrator\Downloads\hmm\ck.py", line 957, in _callEvent
        getattr(self.mgr, evt)(self, *args, **kw)
      File "C:\Users\Administrator\Downloads\hmm\bot.py", line 447, in onMessage
        post("Rolled %s" % str(random.randint(1,n)))
      File "C:\Python34\lib\random.py", line 218, in randint
        return self.randrange(a, b+1)
    TypeError: Can't convert 'int' object to str implicitly

[96m[SAV][0m Saving Rooms..
[STATUS] Setting status Offline...
Waiting 30 seconds for you to read the error..

What I could do and try:

I'm trying to be able to get args to replace random.randint(n) while n = integer. I could try doing n = args.lower() However when I did that it returned a value error since I was unable to get the second option selected, and that would force users to use the command /roll n, n. I thought about trying to set the new code to be random.randint(1,n) Then setting n = args.lower(), that would possibly allow roll selection.

Update

Tried my idea and it simply returned:

TypeError: Can't convert 'int' object to str implicitly

I even proceeded to change the line to post("Rolled %s" % str(random.randint(1,n))) which failed

Oh and if you were wondering this is a roll command for Role Playing purposes Any and all feedback/suggestions are welcomed. I'm hoping to get this resolved.

  • 1
    It would be so much easier to help you if you didn’t just cut the error message but also showed us the whole stack trace with the number of the line that caused the error. – kirelagin May 11 '15 at 05:06
  • @ReutSharabani Post sends a message via the bot the chat room rather than the console. It's hard to make this SSCCE. – user3745348 May 11 '15 at 05:07

3 Answers3

1

randint(a, b) expects two integers but you're giving it an integer and a string.

Instead of converting the result of randint to a string you should convert the second argument to an integer:

    post("Rolled %s" % random.randint(1,int(n)))

This will throw a ValueError if n can't be parsed as an integer (i.e. "/roll foo").

Clarification:

The error message is a bit confusing because what happens when you call random.randint(a,b) is that that method will call random.randrange(a, b+1) without doing any type-checking of it's arguments, causing (C)Python to throw an error about not being able to convert the integer 1 to a string (since b is a string).

You can see this for yourself if you do the following:

Python 3.4.3 (default, Apr 13 2015, 22:30:47) 
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> "8" + 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't convert 'int' object to str implicitly

If we instead try to call random.randrange(a, "9") we get a proper error:

>>> random.randrange(1, "9")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.4/random.py", line 191, in randrange
    raise ValueError("non-integer stop for randrange()")
ValueError: non-integer stop for randrange()

and this is arguably what should happen if we call random.randint(1, "8") too.

Feel free to submit a bug about this to the Python issue tracker

Raniz
  • 10,882
  • 1
  • 32
  • 64
  • AWESOME it worked! I had no idea that that's how it was expecting it. So whenever I do something similar to this I have to add a second argument always? – user3745348 May 11 '15 at 05:16
  • I see. So that explains the `File "C:\Python34\lib\random.py", line 218, in randint return self.randrange(a, b+1)` I was seeing! Thanks for the clarification. I'll make sure to submit this to the issue tracker! Also is there a way to make it where something would prevent the value error? I tried `if args != int: post("Error. Please select an integer")` However, it's posting for integers too. I feel like if I keep that issue there it's incomplete, since it causes a crash. – user3745348 May 11 '15 at 05:51
  • Take a look [here](http://stackoverflow.com/questions/8075877/converting-string-to-int-using-try-except-in-python) for how to do that using try/except – Raniz May 11 '15 at 06:06
0

You need to convert n to an integer before using it inside random.randint(1,n) .

You may use random.randint(1, int(n))

niyasc
  • 4,440
  • 1
  • 23
  • 50
Bidhan
  • 10,607
  • 3
  • 39
  • 50
0

The problem is in this call:

randint(1,n)

Here n is a string, and randint expects it to be an int. I agree that the error message is a little misleading.

kirelagin
  • 13,248
  • 2
  • 42
  • 57