value error in my code (python 3.0) 'chr() arg not in range'












-1















I have to take in a string and an integer value and check if the string is lowercase or uppercase, and based on that I have to increment it by number k. for eg if k=4 and string is 'ABab' it should give the output 'EFef'.



This is my code only for checking lowercase. Unfortunately is giving ValueError.



s=input()
k=int(input())
l=
for i in s:
if i.islower():
if 97>=(ord(i)+k)<=122:
l.append(chr(ord(i)+k))
else:
k=k-122
if 97>=(ord(i)+k)<=122:
l.append((chr(ord(i)+k)))
break
else:
continue
print(l)









share|improve this question





























    -1















    I have to take in a string and an integer value and check if the string is lowercase or uppercase, and based on that I have to increment it by number k. for eg if k=4 and string is 'ABab' it should give the output 'EFef'.



    This is my code only for checking lowercase. Unfortunately is giving ValueError.



    s=input()
    k=int(input())
    l=
    for i in s:
    if i.islower():
    if 97>=(ord(i)+k)<=122:
    l.append(chr(ord(i)+k))
    else:
    k=k-122
    if 97>=(ord(i)+k)<=122:
    l.append((chr(ord(i)+k)))
    break
    else:
    continue
    print(l)









    share|improve this question



























      -1












      -1








      -1








      I have to take in a string and an integer value and check if the string is lowercase or uppercase, and based on that I have to increment it by number k. for eg if k=4 and string is 'ABab' it should give the output 'EFef'.



      This is my code only for checking lowercase. Unfortunately is giving ValueError.



      s=input()
      k=int(input())
      l=
      for i in s:
      if i.islower():
      if 97>=(ord(i)+k)<=122:
      l.append(chr(ord(i)+k))
      else:
      k=k-122
      if 97>=(ord(i)+k)<=122:
      l.append((chr(ord(i)+k)))
      break
      else:
      continue
      print(l)









      share|improve this question
















      I have to take in a string and an integer value and check if the string is lowercase or uppercase, and based on that I have to increment it by number k. for eg if k=4 and string is 'ABab' it should give the output 'EFef'.



      This is my code only for checking lowercase. Unfortunately is giving ValueError.



      s=input()
      k=int(input())
      l=
      for i in s:
      if i.islower():
      if 97>=(ord(i)+k)<=122:
      l.append(chr(ord(i)+k))
      else:
      k=k-122
      if 97>=(ord(i)+k)<=122:
      l.append((chr(ord(i)+k)))
      break
      else:
      continue
      print(l)






      python string






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 17:00









      James Z

      11.1k71835




      11.1k71835










      asked Nov 22 '18 at 15:05









      palakpalak

      94




      94
























          1 Answer
          1






          active

          oldest

          votes


















          1














          The traceback shows where the error occurs.



          Traceback (most recent call last):
          File "C:/Users/rob/test.py", line 11, in <module>
          l.append((chr(ord(i)+k)))
          ValueError: chr() arg not in range(0x110000)


          You are passing an argument to chr that is not within the allowed range. As described here:




          The valid range for the argument is from 0 through 1,114,111 (0x10FFFF
          in base 16). ValueError will be raised if i is outside that range.




          This is because you have changed the value of k to (probably) be a large negative number:



          k=k-122


          So the result of ord(i)+k is also often negative. Negative numbers are not in the allowed range, so the call to chr fails.



          There are lots of other problems with your code, and I don't think you'd learn much if I just wrote "my solution" to the problem. Another thing you might want to look at to begin with is that:



          if 97>=(ord(i)+k)<=122:


          doesn't do what you want, you probably want:



          if 97<=(ord(i)+k)<=122:





          share|improve this answer





















          • 1





            Please note that the range "0-255" is a Python 2 thing only - nowadays most people use Python 3, and CHR and ORD give results in unicode code points. That said, the OP is likely using Python 2, which should be noted in the answer, them.

            – jsbueno
            Nov 22 '18 at 15:35











          • @jsbueno Thanks, good catch, I've updated my answer for Python 3. I'm sure OP is using Python 3 because of the print(l) at the end of the question.

            – Rob Bricheno
            Nov 22 '18 at 15:42













          • @jsbueno I think it's more accurate to say that most people learning Python are using Python 3 (or should be, anyway). The installed base of Python 2 code is still quite large (though probably larger than it should be, with Python 2 support coming to an end).

            – chepner
            Nov 22 '18 at 15:47











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433734%2fvalue-error-in-my-code-python-3-0-chr-arg-not-in-range%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          The traceback shows where the error occurs.



          Traceback (most recent call last):
          File "C:/Users/rob/test.py", line 11, in <module>
          l.append((chr(ord(i)+k)))
          ValueError: chr() arg not in range(0x110000)


          You are passing an argument to chr that is not within the allowed range. As described here:




          The valid range for the argument is from 0 through 1,114,111 (0x10FFFF
          in base 16). ValueError will be raised if i is outside that range.




          This is because you have changed the value of k to (probably) be a large negative number:



          k=k-122


          So the result of ord(i)+k is also often negative. Negative numbers are not in the allowed range, so the call to chr fails.



          There are lots of other problems with your code, and I don't think you'd learn much if I just wrote "my solution" to the problem. Another thing you might want to look at to begin with is that:



          if 97>=(ord(i)+k)<=122:


          doesn't do what you want, you probably want:



          if 97<=(ord(i)+k)<=122:





          share|improve this answer





















          • 1





            Please note that the range "0-255" is a Python 2 thing only - nowadays most people use Python 3, and CHR and ORD give results in unicode code points. That said, the OP is likely using Python 2, which should be noted in the answer, them.

            – jsbueno
            Nov 22 '18 at 15:35











          • @jsbueno Thanks, good catch, I've updated my answer for Python 3. I'm sure OP is using Python 3 because of the print(l) at the end of the question.

            – Rob Bricheno
            Nov 22 '18 at 15:42













          • @jsbueno I think it's more accurate to say that most people learning Python are using Python 3 (or should be, anyway). The installed base of Python 2 code is still quite large (though probably larger than it should be, with Python 2 support coming to an end).

            – chepner
            Nov 22 '18 at 15:47
















          1














          The traceback shows where the error occurs.



          Traceback (most recent call last):
          File "C:/Users/rob/test.py", line 11, in <module>
          l.append((chr(ord(i)+k)))
          ValueError: chr() arg not in range(0x110000)


          You are passing an argument to chr that is not within the allowed range. As described here:




          The valid range for the argument is from 0 through 1,114,111 (0x10FFFF
          in base 16). ValueError will be raised if i is outside that range.




          This is because you have changed the value of k to (probably) be a large negative number:



          k=k-122


          So the result of ord(i)+k is also often negative. Negative numbers are not in the allowed range, so the call to chr fails.



          There are lots of other problems with your code, and I don't think you'd learn much if I just wrote "my solution" to the problem. Another thing you might want to look at to begin with is that:



          if 97>=(ord(i)+k)<=122:


          doesn't do what you want, you probably want:



          if 97<=(ord(i)+k)<=122:





          share|improve this answer





















          • 1





            Please note that the range "0-255" is a Python 2 thing only - nowadays most people use Python 3, and CHR and ORD give results in unicode code points. That said, the OP is likely using Python 2, which should be noted in the answer, them.

            – jsbueno
            Nov 22 '18 at 15:35











          • @jsbueno Thanks, good catch, I've updated my answer for Python 3. I'm sure OP is using Python 3 because of the print(l) at the end of the question.

            – Rob Bricheno
            Nov 22 '18 at 15:42













          • @jsbueno I think it's more accurate to say that most people learning Python are using Python 3 (or should be, anyway). The installed base of Python 2 code is still quite large (though probably larger than it should be, with Python 2 support coming to an end).

            – chepner
            Nov 22 '18 at 15:47














          1












          1








          1







          The traceback shows where the error occurs.



          Traceback (most recent call last):
          File "C:/Users/rob/test.py", line 11, in <module>
          l.append((chr(ord(i)+k)))
          ValueError: chr() arg not in range(0x110000)


          You are passing an argument to chr that is not within the allowed range. As described here:




          The valid range for the argument is from 0 through 1,114,111 (0x10FFFF
          in base 16). ValueError will be raised if i is outside that range.




          This is because you have changed the value of k to (probably) be a large negative number:



          k=k-122


          So the result of ord(i)+k is also often negative. Negative numbers are not in the allowed range, so the call to chr fails.



          There are lots of other problems with your code, and I don't think you'd learn much if I just wrote "my solution" to the problem. Another thing you might want to look at to begin with is that:



          if 97>=(ord(i)+k)<=122:


          doesn't do what you want, you probably want:



          if 97<=(ord(i)+k)<=122:





          share|improve this answer















          The traceback shows where the error occurs.



          Traceback (most recent call last):
          File "C:/Users/rob/test.py", line 11, in <module>
          l.append((chr(ord(i)+k)))
          ValueError: chr() arg not in range(0x110000)


          You are passing an argument to chr that is not within the allowed range. As described here:




          The valid range for the argument is from 0 through 1,114,111 (0x10FFFF
          in base 16). ValueError will be raised if i is outside that range.




          This is because you have changed the value of k to (probably) be a large negative number:



          k=k-122


          So the result of ord(i)+k is also often negative. Negative numbers are not in the allowed range, so the call to chr fails.



          There are lots of other problems with your code, and I don't think you'd learn much if I just wrote "my solution" to the problem. Another thing you might want to look at to begin with is that:



          if 97>=(ord(i)+k)<=122:


          doesn't do what you want, you probably want:



          if 97<=(ord(i)+k)<=122:






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 15:40

























          answered Nov 22 '18 at 15:24









          Rob BrichenoRob Bricheno

          2,325218




          2,325218








          • 1





            Please note that the range "0-255" is a Python 2 thing only - nowadays most people use Python 3, and CHR and ORD give results in unicode code points. That said, the OP is likely using Python 2, which should be noted in the answer, them.

            – jsbueno
            Nov 22 '18 at 15:35











          • @jsbueno Thanks, good catch, I've updated my answer for Python 3. I'm sure OP is using Python 3 because of the print(l) at the end of the question.

            – Rob Bricheno
            Nov 22 '18 at 15:42













          • @jsbueno I think it's more accurate to say that most people learning Python are using Python 3 (or should be, anyway). The installed base of Python 2 code is still quite large (though probably larger than it should be, with Python 2 support coming to an end).

            – chepner
            Nov 22 '18 at 15:47














          • 1





            Please note that the range "0-255" is a Python 2 thing only - nowadays most people use Python 3, and CHR and ORD give results in unicode code points. That said, the OP is likely using Python 2, which should be noted in the answer, them.

            – jsbueno
            Nov 22 '18 at 15:35











          • @jsbueno Thanks, good catch, I've updated my answer for Python 3. I'm sure OP is using Python 3 because of the print(l) at the end of the question.

            – Rob Bricheno
            Nov 22 '18 at 15:42













          • @jsbueno I think it's more accurate to say that most people learning Python are using Python 3 (or should be, anyway). The installed base of Python 2 code is still quite large (though probably larger than it should be, with Python 2 support coming to an end).

            – chepner
            Nov 22 '18 at 15:47








          1




          1





          Please note that the range "0-255" is a Python 2 thing only - nowadays most people use Python 3, and CHR and ORD give results in unicode code points. That said, the OP is likely using Python 2, which should be noted in the answer, them.

          – jsbueno
          Nov 22 '18 at 15:35





          Please note that the range "0-255" is a Python 2 thing only - nowadays most people use Python 3, and CHR and ORD give results in unicode code points. That said, the OP is likely using Python 2, which should be noted in the answer, them.

          – jsbueno
          Nov 22 '18 at 15:35













          @jsbueno Thanks, good catch, I've updated my answer for Python 3. I'm sure OP is using Python 3 because of the print(l) at the end of the question.

          – Rob Bricheno
          Nov 22 '18 at 15:42







          @jsbueno Thanks, good catch, I've updated my answer for Python 3. I'm sure OP is using Python 3 because of the print(l) at the end of the question.

          – Rob Bricheno
          Nov 22 '18 at 15:42















          @jsbueno I think it's more accurate to say that most people learning Python are using Python 3 (or should be, anyway). The installed base of Python 2 code is still quite large (though probably larger than it should be, with Python 2 support coming to an end).

          – chepner
          Nov 22 '18 at 15:47





          @jsbueno I think it's more accurate to say that most people learning Python are using Python 3 (or should be, anyway). The installed base of Python 2 code is still quite large (though probably larger than it should be, with Python 2 support coming to an end).

          – chepner
          Nov 22 '18 at 15:47


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433734%2fvalue-error-in-my-code-python-3-0-chr-arg-not-in-range%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python