![]() |
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. |
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"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. |
while he replied i was working on a solution, you need to learn object oriented programming, its a lot funner ;)
:
class calendarconverter: |
I really didn't feel like teaching him about functions at the same time, let alone classes. :p
|
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). :) |
Quote:
:
>>> import osAs 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 ;) |
But you can use OO where it helps you expand your practice of the language.
|
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. |
Quote:
|
بسم الله
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