mercredi 31 décembre 2014

Python - Novice: Why this simple if-elif (replication of the C case block) always produces the same result?

beginner here. Could anyone explain and/or help me revise this Python code to produce the correct output, whenever I enter the string specified? For example, if March is inputted, output "02". Currently, the program always outputs 00, for January, no matter the input. Here is the code:



x = raw_input("Starting Month: ")

if x == "January" or "january":
stMonth = '00'
elif x == "February" or "february":
stMonth = '01'
elif x == "March" or "march":
stMonth = '02'
elif x == "April" or "april":
stMonth = '03'
elif x == "May" or "may":
stMonth = '04'
elif x == "June" or "june":
stMonth = '05'
elif x == "July" or "june":
stMonth = '06'
elif x == "August" or "august":
stMonth = '07'
elif x == "September" or "september":
stMonth = '08'
elif x == "October" or "october":
stMonth = '09'
elif x == "November" or "november":
stMonth = '10'
elif x == "December" or "december":
stMonth = '11'
else:
print "error"
print stMonth




Output:



$ python month.py
Starting Month: march
00


Thanks in advance - any and all help is much, much appreciated!


Aucun commentaire:

Enregistrer un commentaire