Help please

This is the place for queries that don't fit in any of the other categories.

Help please

Postby coys1717 » Wed Feb 13, 2013 11:08 am

I'm a newbie to coding, working my way through some simple tutorials but am having a spot of bother with a small exercise, to the experienced, the answer should be simple, the scenario is getting the user to input some data, but coding it to check that at least some data has been entered by using the len() function. If the user has entered data then the console prints what has been entered, if no data has been entered then the console is supposed to print "empty". For some reason ive managed to complete the exercise by trying various code, but a) not sure why as it didnt print "empty" while testing and b) why the below code (and its variants wont work)

original = raw_input()
if len(original) < 0:
print "empty"
else:
print original
coys1717
 
Posts: 2
Joined: Wed Feb 13, 2013 10:25 am

Re: Help please

Postby stranac » Wed Feb 13, 2013 11:19 am

Hello!
You should read New Users, Read This, and edit your post accordingly.

Indentation is important in python, and we can't say for sure what the problem is without seeing your indentation.

The only thing I can say for now is:
Code: Select all
if len(original) < 0:

This code checks if the length of the input is less than 0, which will never be True.
You should check if it's equal to 0, or better yet, simply:
Code: Select all
if original:
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 885
Joined: Thu Feb 07, 2013 3:42 pm

Re: Help please

Postby coys1717 » Wed Feb 13, 2013 11:33 am

Thanks for your help, this seems to be working ok now, 2 DAYS! ive been trying to work through this tiny exercise. I'm confused as to why ive had to declare len(original) as cookie as i thought coding " if len(original) <= 0:" was the same thing.

Code: Select all
original = raw_input()
cookie = len(original)
if cookie <= 0:
   print "empty"
else:
   print original


either way getting there!

Thanks
coys1717
 
Posts: 2
Joined: Wed Feb 13, 2013 10:25 am

Re: Help please

Postby stranac » Wed Feb 13, 2013 11:43 am

coys1717 wrote:I'm confused as to why ive had to declare len(original) as cookie as i thought coding " if len(original) <= 0:" was the same thing.

Yes, it is basically the same thing.
But you had < as opposed to <= you have here(it should just be ==, since it's impossible for the length to be a negative number).
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 885
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], metulburr and 2 guests

cron