What are some helpful testing practices?

Question

What are some helpful testing practices?

Answer

How you test your code depends on many different things: program size, complexity of what you’re testing, resources available, your knowledge of what needs to be tested, lots of things! For that reason, it can be difficult to know how to test a program and even more difficult to know when you’ve done enough testing to say with any certainty that the code is ready to go!
As a general rule of thumb, though, for smaller projects like these:
Run your code whenever you add a new feature. That can be as small as a single word of code, a line, or an entire block of code. Introducing something new into a previously working program is a good reason to test!

2 Likes

Why does this code return Empty if my input is: “Hey there” But it works if it is like this: “Heythere” in other words whenever we have a space it returns empty…

print ‘Welcome to the Pig Latin Translator!’

Start coding here!

original = raw_input("Enter a word: ")

if len(original) > 0 and original.isalpha():
print original
else:
print (“Empty”)

3 Likes

Because the space in your input is seen as a character but not and alphabet letter so when .isalpha() is ran it sees that space as a reason to run the else statement and print empty

1 Like

Throughout the entire Pig Latin exercise so far, the website has forced me to use ‘Solution’ to advance instead of giving me the option to press ‘Next’ after I’ve written the proper code. No matter how many times I test my code, and no matter how many variations I make to minor things like double quotes vs. single quotes, spacing, etc., I literally cannot complete each exercise without Codecademy forcing me to ask for the solution. Then, when I click on ‘Solution,’ the website simply produces the exact same code I already have, down to every last little detail. I am wondering what the reason for this buggy behavior might be - any ideas? My only thought so far is that maybe it has something to do with my Internet connection being poor.

EDIT: Switched from Firefox to Chrome and it started working properly. It was a browser issue. Would be nice for someone to be made aware of this incompatibility with Firefox.

8 Likes

Are there similar methods like .isalpha() which can be used to to accept, for example: only integers, or only special characters etc.
I also wonder whether I can use such methods to ensure that the user input essentially contains multiple type of characters (e.g. integer and alphabets are both compulsory) as in the making of strong passwords.

all string methods are listed in the documentation:

https://docs.python.org/3/library/stdtypes.html#string-methods

which include .isdigit(), to check the string contains numbers.

more complex cases are often done with regular expressions.

1 Like

Hi, does isalpha() takes Chinese or French word as each alphabets?
as I am entering French word and it takes. Even the Chinese character

Welcome to the Pig Latin Translator!
Enter a word: année
année

As per function of isalpha() it should take A-Za-z

No.

From the docs:

Alphabetic characters are those characters defined in the Unicode character database as “Letter”, i.e., those with general category property being one of “Lm”, “Lt”, “Lu”, “Ll”, or “Lo”.

More on those “Letters” of which there are more than 120,000, here.

original = raw_input(“Enter a word:”)
if original.isspace() or len(original) <= 0:
print “Empty”
else:
print original

Here’s my updated code which allows for spaces in sentences and well as special characters and integers to be seen as a valid input.

1 Like

Consider,

>>> a = "abc def ghi"
>>> a.isalpha()
False
>>> a.isspace()
False
>>> 

Neither is True because of the mix of alpha and white space.

We can make a list of the entire input, and iterate it by character.

>>> a = "abc1 def2 ghi3"
>>> b = list(a)
>>> c = [x for x in b if x.isalpha() or x.isdigit() or x.isspace()]
>>> c
['a', 'b', 'c', '1', ' ', 'd', 'e', 'f', '2', ' ', 'g', 'h', 'i', '3']
>>> d = ''.join(c)
>>> d == a
True
>>> 

Since the final string is the same as the original one, there were no rejected characters.

>>> a = "$abc1 %def2 &ghi3"
>>> b = [x for x in list(a) if x.isalpha() or x.isdigit() or x.isspace()]
>>> ''.join(b) == a
False
>>> 

As a side note, there is no such thing as a negative length, only a negative displacement. Length is scalar and always positive, or zero in the absence of any.

2 Likes

Hi,
I’m currently working on the Pig Latin translator and I got to the point where I was supposed to type a word into the console, but whenever I try to type, nothing happens. I checked multiple times to make sure my code is correct, and I clicked on the console before I started typing, but nothing comes up when I type on my computer’s keyboard. Could you please help?

1 Like

I can`t write anything in the console.

MY CODE IS THIS
original = raw_input(“Enter a word:”)
if len(original) > 0 and original.isalpha():
print original
else:
print “empty”

I think you have to press the tab key, at the same time that you write the answer