chevron_left chevron_right
Login Register invert_colors photo_library


Stay updated and chat with others! - Join the Discord!
Thread Rating:
  • 0 Vote(s) - 0 Average


VERY first python program filter_list
Author
Message
VERY first python program #1
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.')

Reply

RE: VERY first python program #2
anyone have any suggestions?

Reply

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

RE: VERY first python program #4
(11-27-2012, 11:44 AM)Deque Wrote:
(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

The code did work, but so what you are saying is that I should use elif instead of if's the whole time?

Reply

RE: VERY first python program #5
(11-27-2012, 05:18 PM)LightX Wrote: The code did work, but so what you are saying is that I should use elif instead of if's the whole time?

No, I didn't say that.
I tried to explain that your current checks (if-statements) are inefficient, because you check unnecessarily.
Get to know how if, else and elif work together in order to achieve a better code.
Try to understand by looking at my explanations and pseudocode why it is better.

Maybe I can make up some examples to explain it better. I often feel that my english is not good enough.
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

RE: VERY first python program #6
(11-27-2012, 05:27 PM)Deque Wrote:
(11-27-2012, 05:18 PM)LightX Wrote: The code did work, but so what you are saying is that I should use elif instead of if's the whole time?

No, I didn't say that.
I tried to explain that your current checks (if-statements) are inefficient, because you check unnecessarily.
Get to know how if, else and elif work together in order to achieve a better code.
Try to understand by looking at my explanations and pseudocode why it is better.

Maybe I can make up some examples to explain it better. I often feel that my english is not good enough.

Okay, thank you for the tip! I will look into it! Smile And honestly, i thought you were from the US...i couldnt tell that you werent good with your english...so good job! Smile

Reply

RE: VERY first python program #7
(11-27-2012, 05:31 PM)LightX Wrote: Okay, thank you for the tip! I will look into it! Smile And honestly, i thought you were from the US...i couldnt tell that you werent good with your english...so good job! Smile

Oh, that surprises me. Thanks. I am german, I learned english at school.
Feel free to correct me when I do any mistakes.

And keep up learning Python! It might be small steps at a time, but do it for a while, look back at your beginning in some months and you will be surprised how far you have come. Sometimes your skill increases impalpable.
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

RE: VERY first python program #8
(11-27-2012, 05:39 PM)Deque Wrote:
(11-27-2012, 05:31 PM)LightX Wrote: Okay, thank you for the tip! I will look into it! Smile And honestly, i thought you were from the US...i couldnt tell that you werent good with your english...so good job! Smile

Oh, that surprises me. Thanks. I am german, I learned english at school.
Feel free to correct me when I do any mistakes.

And keep up learning Python! It might be small steps at a time, but do it for a while, look back at your beginning in some months and you will be surprised how far you have come. Sometimes your skill increases impalpable.

Ok, first of all, your english is better than mine! I dont even know what that last word means! XD
But okay, Thanks very much buddy...cant wait to see where I go with this.

Reply

RE: VERY first python program #9
(11-27-2012, 05:42 PM)LightX Wrote: Ok, first of all, your english is better than mine! I dont even know what that last word means! XD
But okay, Thanks very much buddy...cant wait to see where I go with this.

I looked it up in the dictionary, because I didn't knew a proper translation for the german word "unmerklich". So I am not even sure if that fits in this context. Wink
I meant that you often don't realize how much your skill increases.
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

RE: VERY first python program #10
(11-27-2012, 05:46 PM)Deque Wrote:
(11-27-2012, 05:42 PM)LightX Wrote: Ok, first of all, your english is better than mine! I dont even know what that last word means! XD
But okay, Thanks very much buddy...cant wait to see where I go with this.

I looked it up in the dictionary, because I didn't knew a proper translation for the german word "unmerklich". So I am not even sure if that fits in this context. Wink
I meant that you often don't realize how much your skill increases.

Ah, ok....Hey i guess i know some german now too! Biggrin

Reply






Users browsing this thread: 1 Guest(s)