Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Are there any alternatives for GOTO in Python?I am facing a problem please respond. (http://www.programmingforums.org/showthread.php?t=5398)

verb1426 Aug 14th, 2005 4:01 PM

Are there any alternatives for GOTO in Python?I am facing a problem please respond.
 
بسم الله الرحمن الرحيم
Hi guys

I have designed a program that converts the date from Islamic calendar to Christian calendar.Unfortunately, I have faced a REAL problem (I mean a REAL problem for me as a beginner). First Here is the program:


print " Welcome to date converting program"
print " ------------------------------------"
print
print " Please choose the number for the converting process you wish to perform:"
print
print "1- From Christian calendar to Islamic calendar."
print "2- From Islamic calendar to Christian calendar."
print
a = input ("The number:")
if a == 1:
d = input ("Enter the date:")
g = d - 579
if g <0:
print "The date in the Islamic calendar is" , g - g - g , "BH"
else:
print "The date in the Islamic calendar is" , g , "AH"
elif a == 2:
d = input ("Enter the date:")
print "The date in the Christian calendar is" , d + 579 , "AD"
else:
print "Restart the program and enter the right number."


-------------------------
But as you can see, since there is no GOTO function in Python, I had to write :"Restart the program and enter the right number."

I would appreciate it if anyone could modefy this program or show me how to modefy it so that if I choose a number that is not on the list it returns to the line which says: a = input ("The number:")

Thank you.

Ooble Aug 14th, 2005 4:22 PM

First of all, especially when posting Python code, use [ code ] tags. Without them, the webpage eats whitespace.

Secondly, don't use one-letter variables. It makes debugging a bitch.

Onto the code. I've improved it where I could. The trick is to use a loop:
:

print " Welcome to my Date-Converting Application"
print " -----------------------------------------"
print
print " Please the converting process you wish to perform:"
print
print "  1: From Christian calendar to Islamic calendar."
print "  2: From Islamic calendar to Christian calendar."
print "  0: Exit."
print

a = 42

while a != 0:
        a = input ("Conversion: ")
        if a == 0:
                break
       
        if a == 1:
                d = input ("Enter the date: ")
                g = d - 579
               
                if g < 0:
                        print "The date in the Islamic calendar is", g - g - g, "BH"
                else:
                        print "The date in the Islamic calendar is", g, "AH"
       
        elif a == 2:
                d = input ("Enter the date: ")
                print "The date in the Christian calendar is", d + 579, "AD"
       
        else:
                print "Not an option, genius."
       
        print


By the way, using a goto function is almost always considered bad programming. There's almost always a better way to do things, unless you're coding in Assembly.

coldDeath Aug 14th, 2005 4:37 PM

while he replied i was working on a solution, you need to learn object oriented programming, its a lot funner ;)

:

class calendarconverter:
        def christiantoislamic(self):
                idate = input("Enter the Christian date:")
                g = idate -579
                if g <0:
                        print "The date in the Islamic calendar is" , g - g - g , "BH"
                else:
                        print "The date in the Islamic calendar is" , g , "AH"
               
        def islamictochristian(self):
                cdate = input ("Enter the date:")
                print "The date in the Christian calendar is" , cdate + 579 , "AD"
               
        def run(self):
                cal=input("1 or 2:")
                if (cal == 1):
                        self.christiantoislamic()
                elif (cal == 2):
                        self.islamictochristian()
                else:
                        print"Use correct syntax please"
                        self.run()
               
calendar = calendarconverter()
calendar.run()

works like a dream

Ooble Aug 14th, 2005 4:48 PM

I really didn't feel like teaching him about functions at the same time, let alone classes. :p

Sane Aug 14th, 2005 6:00 PM

Holy crap ColdDeath! You're improving quickly!! :eek:

P.S.

Code like this:

print "The date in the Islamic calendar is" , g , "AH"

Can be done with better possibilities like this:

print "The date in the Islamic calendar is %s AH"%( g )

Where %s requests a string, and %d requests an integer. But %s is recommended in all situations even if you have an integer.


And if you are looking to make your program more user friendly, raw_input superceeds input, and then in the condition testing you must make 1 in to "1" or make cal in to str(cal).

:)

Cerulean Aug 14th, 2005 6:27 PM

Quote:

And if you are looking to make your program more user friendly, raw_input superceeds input
And if you want to make your program safe. input() calls eval on what it reads, meaning you can do:
:

>>> import os
>>> input("Enter a number: ")
os.remove("/")
(goodbye computer if you did that as root)

Yes, it's that unsafe.

As for the object-orientated example.. the program gains nothing from the OO. This isn't Java - use OO where it makes sense to do so, not because it looks cool ;)

Sane Aug 15th, 2005 12:20 AM

But you can use OO where it helps you expand your practice of the language.

coldDeath Aug 15th, 2005 6:24 AM

Quote:

Holy crap ColdDeath! You're improving quickly!!
=) thanks, and i just spent quite a while learning Tkinter, i can do it pretty well now, then i find everyone says wxPython is better :o lol

I must admit though, Tkinter is disgustingly ugly on linux, i mean have you seen the menus it makes (like the File,Edit,View menu). I did a small bit of wxpython and it does look better.

Dietrich Aug 15th, 2005 8:07 AM

Quote:

Originally Posted by Cerulean
And if you want to make your program safe. input() calls eval on what it reads, meaning you can do:
:

>>> import os
>>> input("Enter a number: ")
os.remove("/")
(goodbye computer if you did that as root)

Yes, it's that unsafe.

As for the object-orientated example.. the program gains nothing from the OO. This isn't Java - use OO where it makes sense to do so, not because it looks cool ;)

Too bad input() has never been modified to trap this kind of silliness!

verb1426 Aug 15th, 2005 10:08 AM

بسم الله
Actually I don't know how to thank you .....this is very kind of you ......I realize that programming requires cleverness , logical thinking and finally (and most importantly) good knowledge in mathmatics.Such qualities are available in you guys ...but for me, I am ZERO in maths.I can't even tell what 3**4(for instance) is except if I do it on paper or after long thinking ..and even if I know I can't put it in a fruitful program. Can I still become professional with my poor knowledge in maths? I maen gradually?
Thank you for everything.


All times are GMT -5. The time now is 2:32 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC