Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 30th, 2012, 12:25 AM
ika1020 ika1020 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 1 ika1020 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 5 sec
Reputation Power: 0
Post Really easy assignment... yet I can't do it.

I'm new to python, and for my assignment, I have to write a program, that when the wrong number of digits is given (in this case anything but 4-digits), it prints an error message. I have to set the boolean variable set valid to false & then stop.

It also has to give an error message if there are non-digits in my program. again, set valid to false & then stop.

does anyone have any help that could get me started?

Reply With Quote
  #2  
Old September 30th, 2012, 05:24 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is online now
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,380 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 13 h 7 m 19 sec
Reputation Power: 383
Code:
import sys
import string

class HomeWorkError(ValueError):
    pass

sys.stdout.write('Please enter 4 digits.\n')
a = sys.stdin.readline().strip()
valid = set(a).issubset(set(string.digits)) and (4 == len(a))
if not valid:
    raise HomeWorkError('Digits!  Four characters!')

You're right, it's easy. Untested. Most of my untested codes fail.

Note that I've written this in a style that works with python2 or python3.
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old October 2nd, 2012, 06:07 PM
norm850 norm850 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 15 norm850 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 13 m 33 sec
Reputation Power: 0
Quote:
Originally Posted by ika1020
I'm new to python, and for my assignment, I have to write a program, that when the wrong number of digits is given (in this case anything but 4-digits), it prints an error message. I have to set the boolean variable set valid to false & then stop.

It also has to give an error message if there are non-digits in my program. again, set valid to false & then stop.

does anyone have any help that could get me started?


Not sure if you still need help, or if the above reply helped, but I would do it like this:

If given a number, and we want to return false if it is not 4 digits, then divide the number by 1000, and if the value is greater than or equal to 1, then we know it at least 4 digits, and if the value is greater than or equal to 10, we know it is more than 4 digits.

Hope that can help!

Reply With Quote
  #4  
Old October 2nd, 2012, 06:52 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is online now
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,380 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 13 h 7 m 19 sec
Reputation Power: 383
Yes,
int(math.log10(x)) == 3
is also a test.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Really easy assignment... yet I can't do it.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap