Ascii Classifier

For students who are taking a programming course, may be restricted in what they can use, or have other unusual limitations on solving their task.

Ascii Classifier

Postby nzieno » Wed Sep 21, 2016 11:08 pm

ASCII Classifier. Save program as ascii_classifier.py. Write a program that asks the user for a single character, and based on its ASCII value the program determines whether this character is:
A control character (ASCII values between 0 and 31),
A digit,
An uppercase letter,
A lowercase letter, or
A symbol (if it does not fit in the categories above)

Code: Select all
SingleCharacter = input("Enter a character:",)
Character = ord(SingleCharacter)

if Character > 0 and Character <= 31:
    print("This is a control character")
elif Character >= 48 and Character <= 57:
    print("This is a character is a digit")
elif Character >= 65 and Character <= 90:
    print("This is a uppercase letter")
elif Character >= 97 and Character <= 122:
    print("This is a lowercase letter")
else:
    print("This is a Symbol")


so this is what I have so far I can get a response for all the IF statements except the first IF. Could anybody shed some light on why this is please?
nzieno
 
Posts: 16
Joined: Fri Jul 29, 2016 12:38 am

Re: Ascii Classifier

Postby micseydel » Wed Sep 21, 2016 11:11 pm

What input have you tried? Have you tried something that doesn't work, or are you not sure what to try?
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
micseydel
 
Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Ascii Classifier

Postby sparkz_alot » Thu Sep 22, 2016 3:03 pm

You have a fundamental problem, and that is the control characters are 'control sequences', using the control key and a character, for example [control]G rings the bell. These characters are not on todays standard keyboards (ascii was originally for teletypes). Since you can't enter them with a single key, you will not meet the criteria of 'ord' (being a single character).

Not sure where you are in your class, but using a 'try/except' and catching the "TypeError", though I'm not sure how you would test it.
THIS FORUM WILL BE CLOSING OCT 1, 2016. PLEASE STOP BY AND REGISTER AT THE NEW LOCATION
http://python-forum.io
User avatar
sparkz_alot
 
Posts: 269
Joined: Sat Dec 06, 2014 2:55 pm
Location: US

Re: Ascii Classifier

Postby nzieno » Thu Sep 22, 2016 6:11 pm

Thanks for the reply's! That is what I read online as well so I'm not really sure what my professor is expecting from that kind of input.
nzieno
 
Posts: 16
Joined: Fri Jul 29, 2016 12:38 am

Re: Ascii Classifier

Postby micseydel » Thu Sep 22, 2016 7:54 pm

It is testable though. Here's a demo terminal session:
Code: Select all
$ python -c "print '\1'" | python3 testit.py
Enter a character:This is a control character
$ cat testit.py
SingleCharacter = input("Enter a character:",)
Character = ord(SingleCharacter)

if Character > 0 and Character <= 31:
    print("This is a control character")
elif Character >= 48 and Character <= 57:
    print("This is a character is a digit")
elif Character >= 65 and Character <= 90:
    print("This is a uppercase letter")
elif Character >= 97 and Character <= 122:
    print("This is a lowercase letter")
else:
    print("This is a Symbol")

Note that the null byte is counted as a "symbol".
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
micseydel
 
Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Ascii Classifier

Postby sparkz_alot » Thu Sep 22, 2016 10:11 pm

micseydel wrote:It is testable though. Here's a demo terminal session:
Code: Select all
$ python -c "print '\1'" | python3 testit.py
Enter a character:This is a control character
$ cat testit.py
SingleCharacter = input("Enter a character:",)
Character = ord(SingleCharacter)

if Character > 0 and Character <= 31:
    print("This is a control character")
elif Character >= 48 and Character <= 57:
    print("This is a character is a digit")
elif Character >= 65 and Character <= 90:
    print("This is a uppercase letter")
elif Character >= 97 and Character <= 122:
    print("This is a lowercase letter")
else:
    print("This is a Symbol")

Note that the null byte is counted as a "symbol".


That might be true and useful to the instructor, but the user only has the option of:
Code: Select all
SingleCharacter = input("Enter a character:",)


Unless the instructor merely wants to see if the code exists to handle control characters :?: . Your right, that line should be corrected to include the 'null' character
THIS FORUM WILL BE CLOSING OCT 1, 2016. PLEASE STOP BY AND REGISTER AT THE NEW LOCATION
http://python-forum.io
User avatar
sparkz_alot
 
Posts: 269
Joined: Sat Dec 06, 2014 2:55 pm
Location: US

Re: Ascii Classifier

Postby micseydel » Thu Sep 22, 2016 10:22 pm

I'm not denying that you can't use a standard keyboard character to produce those characters. I was just trying to hint strongly that the OP's code isn't correct according the specifications. If there's a gradebot, then they might fail that case.

In college, I actually had a class where something like this happened. The professor's test cases didn't test for these characters, however we were given the opportunity to provide inputs to break each other's programs. The simplest break was people's programs not handling the null character properly, which is what is happening here.

If it's in the specs, it should be handled as such.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
micseydel
 
Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Ascii Classifier

Postby nzieno » Thu Sep 22, 2016 11:07 pm

all the instructions say are that the control characters are ascii 0-31 if I lose points on that portion ill go fight the grade and just see if I can fix it other than that it looks as though I did what was asked of in the problem.

Thanks again for all the posts
nzieno
 
Posts: 16
Joined: Fri Jul 29, 2016 12:38 am

Re: Ascii Classifier

Postby micseydel » Thu Sep 22, 2016 11:31 pm

Why fight when it's an easy correction and the instructions were clear?
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
micseydel
 
Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Ascii Classifier

Postby sparkz_alot » Fri Sep 23, 2016 1:53 pm

all the instructions say are that the control characters are ascii 0-31


What does your code say?
THIS FORUM WILL BE CLOSING OCT 1, 2016. PLEASE STOP BY AND REGISTER AT THE NEW LOCATION
http://python-forum.io
User avatar
sparkz_alot
 
Posts: 269
Joined: Sat Dec 06, 2014 2:55 pm
Location: US


Return to Homework

Who is online

Users browsing this forum: No registered users and 3 guests