Login Register






VERY first python program filter_list
Author
Message
RE: VERY first python program #3
(11-26-2012, 07:59 AM)LightX Wrote: Hey guys. I, with a little help of a friend, mad my very first program using python. It is a VERY simple program that is pretty much useless, but it is a start! Smile
Here is the code for it. Please let me know what you think of it for my first code. Please dont hate on it! Smile
Code:
import datetime #Variables now = datetime.datetime.now() minute = now.minute hour = now.hour second = now.second month = now.month day = now.day year = now.year #Code user_input = raw_input ('Would you like to know date, time, or both?\n') if (user_input == 'date') : print str(month)+"/"+str(day)+"/"+str(year) if (user_input == 'time') : print str(hour)+":"+str(minute)+":"+str(second) if (user_input == 'both') : print str(month)+"/"+str(day)+"/"+str(year)+", "+str(hour)+":"+str(minute)+":"+str(second) elif (user_input != 'date') & (user_input != 'time') & (user_input != 'both') : print ('Sorry, try again.')

Congrats to your very first python program.

I assume your code works fine, but there seems to be a misconception about if-else-elif statements.
Now imagine the user inputs 'date'. This is what will happen:

Code:
if (user_input == 'date') : print str(month)+"/"+str(day)+"/"+str(year)

The computer checks if input == 'date'. Since this is true, it will print the string.
Now we already know that 'date' is the input. Nevertheless the computer will check for 'time' here:

Code:
if (user_input == 'time') : print str(hour)+":"+str(minute)+":"+str(second)

And check for 'both' here:

Code:
if (user_input == 'both') : print str(month)+"/"+str(day)+"/"+str(year)+", "+str(hour)+":"+str(minute)+":"+str(second)

Both conditions are false, so nothing is printed. But the checking itself is unnecessary at this point.
Since (user_input == 'both') is false, the elif-statement is also checked:

Code:
elif (user_input != 'date') & (user_input != 'time') & (user_input != 'both') : print ('Sorry, try again.')

The elif only belongs to the last if statement, so only if user_input == 'both' is true, the computer doesn't check for the condition in elif. But it will check for it if the user_input is something else than 'both', even if it is something valid like 'date' or 'time'.
That also means, that user_input != 'both' is redundand. user_input has to be something else than 'both' otherwise it wouldn't go into elif in the first place.

What you really want to do is:
check for 'date' and only if that is false
check for 'time' and only if that is false
check for 'both' and only if that is also false
do something else

You can do this by using if-elif-else-statements.

Let me write a bit pseudocode:

Code:
if input == 'date': print something else if input == 'time': print something else if input == 'both': print something else print error message
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply





Messages In This Thread
VERY first python program - by LightX - 11-26-2012, 07:59 AM