Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
bug in my code
#1
when i run the code on my laptop it runs fine. but when i try to submit online it detects a bug with online Type Error  . what can i do to correct the code. the code is suppose to check for isogram, raise a typeerror if non string input is supplied and also return a false if an empty string is entered .
my code 


def is_isogram (saw):
  cat = str(saw)
  while True:
    if len(cat) < 1 :
      return ("(argument, False)")
    elif not cat.isalpha():
      raise TypeError ("\'Argument should be a string'")
    else:
      for me in cat:
        cat = cat.lower()
        if cat.count(me) > 1:
          return (cat, False)
      return (cat, True)
Reply
#2
Related: https://python-forum.io/Thread-writing-a...or-isogram

(Feb-03-2017, 08:42 PM)sunnysude Wrote:
      raise TypeError ("\'Argument should be a string'")

Why does your string have a backslash in it?  Also, why does your error nest an extra pair of quotes within it?
I mean, if the error you're getting is the TypeError, you should probably look at your type error to see what you're doing :P
Reply
#3
It all depends where you get saw from. If you are using a wrong encoding, bytes for accented characters could be converted to the wrong characters.

Also,
  • "(argument, False)" is not a tuple, but a string
  • "\'Argument should be a string'" is strange (why the simple quotes, and why escaping only one?)
  • cat = cat.lower() should be done only once (in fact, there is a bug if your first letter is an uppercase... do you see why?)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#4
(Feb-03-2017, 09:12 PM)
Ofnuts Wrote: 
It all depends where you get saw from. If you are using a wrong encoding, bytes for accented characters could be converted to the wrong characters.

Also,
  • "(argument, False)" is not a tuple, but a string
  • "\'Argument should be a string'" is strange (why the simple quotes, and why escaping only one?)
  • cat = cat.lower() should be done only once (in fact, there is a bug if your first letter is an uppercase... do you see why?)

have changed the code and removed the quotes and back slash but am still getting the error how can i remove the bug . the one u found with the uppercase ?




Quote:def is_isogram (word):
 cat = str(word)
 while True:
   if len(cat) < 1 :
     return ("argument, False")
   elif not cat.isalpha():
     raise TypeError ("Argument should be a string")
   else:
     for me in cat:
       cat = cat.lower()
       if cat.count(me) > 1:
         return (cat, False)
     return (cat, True)
Reply
#5
Please add code tags so indentation is preserved (and because it's a forum rule)
Reply
#6
(Feb-03-2017, 09:05 PM)nilamo Wrote: Related: https://python-forum.io/Thread-writing-a...or-isogram

(Feb-03-2017, 08:42 PM)sunnysude Wrote:
      raise TypeError ("\'Argument should be a string'")

Why does your string have a backslash in it?  Also, why does your error nest an extra pair of quotes within it?
I mean, if the error you're getting is the TypeError, you should probably look at your type error to see what you're doing :P
have removed the backlash but am still getting the error

(Feb-05-2017, 12:29 PM)Larz60+ Wrote: Please add code tags so indentation is preserved (and because it's a forum rule)

ok was not aware . but have done that now
Reply
#7
You are getting an error, because you are telling Python to generate an error, so Python says "okee dokee here's an error"

If you don't want that, try something like:

   elif not cat.isalpha():
     print("not alpha")
     break
Just a matter of semantics, but your response "Argument should be a string", is not really accurate,  since at the beginning of your function you are converting every thing to a string.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#8
(Feb-05-2017, 02:58 PM)sparkz_alot Wrote: You are getting an error, because you are telling Python to generate an error, so Python says "okee dokee here's an error"

If you don't want that, try something like:

   elif not cat.isalpha():
     print("not alpha")
     break
Just a matter of semantics, but your response "Argument should be a string", is not really accurate,  since at the beginning of your function you are converting every thing to a string.

according to the assignment question am suppose to raise a type error here is the full question

Write a program that checks if a word supplied as the argument is an Isogram. An Isogram is a word in which no letter occurs more than once.

Create a method called is_isogram that takes one argument, a word to test if it's an isogram. This method should return a tuple of the word and a boolean indicating whether it is an isogram.

If the argument supplied is an empty string, return the argument and False: (argument, False). If the argument supplied is not a string, raise a TypeError with the message 'Argument should be a string'.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020