Cannot understand the code.

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

Cannot understand the code.

Postby Python pain » Mon May 12, 2014 3:17 pm

Hii guys
I just coded a really simple software which tell us whether the number you entered is even or odd.
Heres the code:
Code: Select all
import os
def mainmenu():
 X=input("Enter a number: ")
 B=X%2
 if B == 0:
    print"Number is even"
 else:
    print"number is odd"
 print"\n\n1.Press 1 to return to mainmenu: "
 choice=int(raw_input("Press: "))
 os.system("cls")
status = 1
while status !=-1:
   status = mainmenu()


Now i wanted to add this return to mainmenu option in my code, so i downloaded a python based software which already has that and copied the particular code from there to my software.
the thing i copied are:
Code: Select all
def mainmenu():


Code: Select all
print"\n\n1.Press 1 to return to mainmenu: "
 choice=int(raw_input("Press: "))


Code: Select all
status = 1
while status !=-1:
   status = mainmenu()

Now as i copied it,I do not know how this works and also i do not want to just remember it and use it every time. i want to understand it so that i can modify it and use it in other places also.
I know how to use "def" function but i am not getting that how it is used here.
Please please please explain me guys.
Last edited by Mekire on Mon May 12, 2014 3:27 pm, edited 1 time in total.
Reason: First post lock.
Python pain
 
Posts: 2
Joined: Mon May 12, 2014 3:00 pm

Re: Cannot understand the code.

Postby 7stud » Mon May 12, 2014 6:36 pm

First things first:

1) Don't capitalize variable names.
2) Use descriptive variable names. 'X' is not descriptive.
3) Python indenting is four spaces--not one space.
4) Use blank lines after function definitions to separate them from the rest of your code.

This code:

Code: Select all
status = 1
while status !=-1:
   status = mainmenu()


...says to keep executing the function mainmenu() over and over again until mainmenu() returns -1. If mainmenu() returns -1, then status will be assigned -1, which will cause the while condition to fail, ending the while loop. However....your mainmenu() function does not have a return statement, and when a function does not have a return statement it returns None by default, which means status will continually be assigned the value None making your while loop an infinite loop that never ends.

Finally, the cls command is a windows command and won't work on other systems, so your users need to have Windows installed. Is that always the case? Typically, you want to try to write programs that will work on any system.
7stud
 
Posts: 85
Joined: Wed Apr 02, 2014 2:36 am

Re: Cannot understand the code.

Postby Python pain » Tue May 13, 2014 5:29 am

Hii,
Thank you for your effort.
Actually the point is that the python language is no where in my course.
I am a biology student but i am interested in computers a lot.
I do not have any teacher and i am struggling to learn this language from internet which is not going so good for me.
Please tell me a website or something from which i can gain complete knowledge of python.
Last edited by Mekire on Tue May 13, 2014 7:09 am, edited 1 time in total.
Reason: Quote removed.
Python pain
 
Posts: 2
Joined: Mon May 12, 2014 3:00 pm

Re: Cannot understand the code.

Postby Kebap » Tue May 13, 2014 10:55 am

Hey pain, welcome to the forums and to python! :) Glad you have a try. Feel free to ask more if you have any specific questions.

Did you already have a look at our tutorial section? We have collected a huge List of free python ressources there for you.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 375
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 6 guests