Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 14th, 2005, 4:01 PM   #1
verb1426
Newbie
 
Join Date: Aug 2005
Posts: 26
Rep Power: 0 verb1426 is on a distinguished road
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.
verb1426 is offline   Reply With Quote
Old Aug 14th, 2005, 4:22 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Aug 14th, 2005, 4:37 PM   #3
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 3 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
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
coldDeath is offline   Reply With Quote
Old Aug 14th, 2005, 4:48 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I really didn't feel like teaching him about functions at the same time, let alone classes. :p
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Aug 14th, 2005, 6:00 PM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
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).

Sane is offline   Reply With Quote
Old Aug 14th, 2005, 6:27 PM   #6
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
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
Cerulean is offline   Reply With Quote
Old Aug 15th, 2005, 12:20 AM   #7
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
But you can use OO where it helps you expand your practice of the language.
Sane is offline   Reply With Quote
Old Aug 15th, 2005, 6:24 AM   #8
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 3 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
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.
coldDeath is offline   Reply With Quote
Old Aug 15th, 2005, 8:07 AM   #9
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

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!
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Aug 15th, 2005, 10:08 AM   #10
verb1426
Newbie
 
Join Date: Aug 2005
Posts: 26
Rep Power: 0 verb1426 is on a distinguished road
بسم الله
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.
__________________
www.islam.ws

Last edited by verb1426; Aug 15th, 2005 at 10:37 AM.
verb1426 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:39 AM.

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