[Tutor] Question about calling functions

Ara Kooser ghashsnaga at gmail.com
Sun Aug 26 23:41:30 CEST 2007


Hello,

  After taking some suggestions from bhaaluu my program for generating
a character works except for one part. You have a choice of excepting
a set of numbers. If you choose n then the program dumps you into:
def navy() instead of going back to def upp().

I am guessing this has to do with how I am calling the functions at
the end of the program:

###############################################################################
# start program here
###############################################################################
print """Welcome to the Classic Traveller character generator.
        Written in Python"""
print """Press return to generate a character"""
raw_input()

print upp()
print """You have a chance to reroll if needed."""
reroll()
navy()

I see that after reroll() it goes to navy(). What I don't understand
is why when you have a choose n it  won't go back to upp(). Thanks.

Ara



PROGRAM BELOW
##############################################################################################
# Version:0.3
# By: Ara Kooser
#############################################################################################
import random

strength = 0
dexterity = 0
endurance = 0
intelligence = 0
education = 0
social = 0




print """\nGenerating your UPP."""
print

def upp():
   global strength
   global dexterity
   global endurance
   global intelligence
   global education
   global social

   strength = random.randrange(2,12)
   dexterity = random.randrange(2,12)
   endurance = random.randrange(2,12)
   intelligence = random.randrange(2,12)
   education = random.randrange(2,12)
   social = random.randrange(2,12)

   return strength, dexterity, endurance, intelligence, education, social


def reroll():
   a = raw_input("Are you satisfied with your UPP? Choose yes or no.").lower()
   try:
       if a[0] == "y":
           career()
       elif a[0] == "n":
           upp()
       else:
           print "Please choose a valid option."
           print
           out = reroll()

   except:
       print "Please choose a valid option."
       print
       out = reroll()

   return

def career():

   print """You will now choose a career path for your character."""
   print """Please choose your career path."""
   print

   b = raw_input("Navy, Marines, Army, Scouts, Merchants").lower()
   try:
       if b[0] == "navy":
           out = navy()

       elif b[0] == "marines":
           marines()

       elif b[0] == "army":
           army()

       elif b[0] == "scouts":
           scouts()

       elif b[0] == "merchants":
          merchants()

   except:
      print "Please choose a valid option."
      print
      career()

   return

def navy():
   global strength
   global dexterity
   global endurance
   global intelligence
   global education
   global social

   print """You have selected a naval career."""
   c = raw_input("How many terms of service do you want? 1,2,3,4,5")

   enlist = int(c)
   count = 0

   rank = 0
   age = 18
   benefits = []
   cash = []
   skills = []
   commission = False

   while count<enlist:
      age=age+4
      count=count+1

      if commission == False:
         comm = random.randrange(2,12)
         if comm>=10:
            commission = True
            print "You made commission"
         else:
            print "You did not make comission"


      if commission == True:
         prom = random.randrange(2,12)
         if prom>=8:
            rank=rank+1
            print "Your rank is now"
            print rank
         else:
            print "You did not make promotion"


      pskill = random.randrange(1,6)
      if pskill == 1:
         strength=strength+1

      elif pskill == 2:
         dexterity=dexterity+1

      elif pskill == 3:
         endurance=endurance+1

      elif pskill == 4:
         intelligence=intelligence+1

      elif pskill == 5:
         education=education+1

      elif pskill == 6:
         social=social+1


      sskill = random.randrange(1,6)
      if sskill == 1:
         skills[1:1]=['Ships Boat']

      elif sskill == 2:
         skills[1:1]=['Vacc Suit']

      elif sskill == 3:
         skills[1:1]=['Fwd Obsvr']

      elif sskill == 4:
         skills[1:1]=['Gunnery']

      elif sskill == 5:
         skills[1:1]=['Blade Cbt']

      elif sskill == 6:
         skills[1:1]=['Gun Cbt']

      if education<8:
         aeskill = random.randrange(1,6)
         if aeskill == 1:
            skills[1:1]=['Vacc Suit']

         elif aeskill == 2:
            skills[1:1]=['Mechanical']

         elif aeskill == 3:
            skills[1:1]=['Electronic']

         elif aeskill == 4:
            skills[1:1]=['Engineering']

         elif aeskill == 5:
            skills[1:1]=['Gunnery']

         elif aeskill == 6:
            skills[1:1]=['Jack-o-T']


      if education>=8:
         ae8skill = random.randrange(1,6)
         if ae8skill == 1:
            skills[1:1]=['Medical']

         elif ae8skill == 2:
            skills[1:1]=['Navigation']

         elif ae8skill == 3:
            skills[1:1]=['Engineering']

         elif ae8skill == 4:
            skills[1:1]=['Computer']

         elif ae8skill == 5:
            skills[1:1]=['Pilot']

         elif ae8skill == 6:
            skills[1:1]=['Admin']






   print
   print"#################################"
   print "# Your UPP is:",strength, dexterity, endurance,
intelligence, education, social
   print "# Your current rank is now:",rank
   print "# Your current age is now:",age
   print "#",skills
   print"#################################"

   return


###############################################################################
# start program here
###############################################################################
print """Welcome to the Classic Traveller character generator.
        Written in Python"""
print """Press return to generate a character"""
raw_input()


print upp()
print """You have a chance to reroll if needed."""

reroll()
navy()











-- 
Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis
an sub cardine glacialis ursae.


More information about the Tutor mailing list