![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Aug 2005
Posts: 26
Rep Power: 0
![]() |
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 " Please choose the number for the converting process you wish to perform:" print "1- From Christian calendar to Islamic calendar." print "2- From Islamic calendar to Christian calendar." 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. |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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."
printBy 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. |
|
|
|
|
|
#3 |
|
Expert Programmer
|
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() |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
I really didn't feel like teaching him about functions at the same time, let alone classes. :p
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
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). ![]() |
|
|
|
|
|
#6 | |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Quote:
>>> import os
>>> input("Enter a number: ")
os.remove("/")
(goodbye computer if you did that as root)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 ![]() |
|
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
But you can use OO where it helps you expand your practice of the language.
|
|
|
|
|
|
#8 | |
|
Expert Programmer
|
Quote:
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. |
|
|
|
|
|
|
#9 | |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Quote:
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
|
#10 |
|
Newbie
Join Date: Aug 2005
Posts: 26
Rep Power: 0
![]() |
بسم الله
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|