1

I have created a small program to see if I'm as proficient in Python as I am in FreeBasic (and I'm not that good with FreeBasic). Obviously, I'm asking this question because the answer is no.

So the this program is a small Dungeons and Dragons (2nd edition) combat generator. For some reason, many functions don't execute at all. They are simply skipped over. This is what happens with attaque1(), attaque2() and most likely with calcInitiative() (since the cnt variable is not incremented at all). I tried globalizing a lot of variables thinking this might be the issue (I think all variables are globalized by default with FreeBasic). Well, this doesn't seem to be the answer. The bug is still there and I have absolutely no idea what might cause it.

(The code has some French in it.)

#-*- coding: iso8859_1 -*-

import random

ca1 = 10
ca2 = 10
taco = 20
pv1 = random.randint(1,10)
pv1 = (pv1)
pv2 = random.randint(1,10)
pv2 = str(pv2)
cnt = 0
pv1Depart = pv1
pv2Depart = pv2
ast = "*" * 7
d20_1 = random.randint(1,20)
d8_1 = random.randint(1,8)
d20_2 = random.randint(1,20)
d8_2 = random.randint(1,8)

def intro():
    global ca1
    global ca2
    global pv1
    global pv2

    print "Imaginez deux guerriers de D&D 2e édition qui se battent."
    print
    print "Guerrier 1: " + str(pv1) + " PV, épée longue (1-8 points de dégât), TACO de 20, CA de " + str(ca1) + "."
    print "Guerrier 2: " + str(pv2) + " PV, épée longue (1-8 points de dégât), TACO de 20, CA de " + str(ca2) + "."
    print ""

def nouveauCombat():
    global cnt

    print ast + "NOUVEAU COMBAT" + ast
    print
    while ((pv1 <= 0) or (pv2 <= 0)):
        cnt = cnt + 1
        print ast + "ROUND " + str(cnt) + ast
        print
        calcInitiative()
        print
    print ast + "RESULTAT" + ast
    print
    resultat()

def calcInitiative():
    global pv1
    global pv2
    global initiative1
    global initiative2

    initiative1 = random.randint(1,10)
    initiative2 = random.randint(1,10)
    print "Le guerre 1 fait son jet d'initiative."
    print str(initiative1) + "!"
    print
    print "Le guerre 2 fait son jet d'initiative."
    print str(initiative2) + "!"
    print
    if initiative1 == initiative2:
        print "Les deux guerriers attaquent au même moment."
        print
        print ast + "ATTAQUE" + ast
        print
        attaque1()
        print
        attaque2()
    elif initiative1 < initiative2:
        print "Le guerrier 1 attaque en premier."
        print
        print ast + "ATTAQUE" + ast
        print
        attaque1()
        print
        if pv2 <= 0:
            print
            attaque2()
    else:
        print "Le guerrier 2 attaque en premier."
        print
        print ast + "ATTAQUE" + ast
        print
        attaque2()
        print
        if pv1 <= 0:
            print
            attaque2()

def attaque1():
    global d20_1
    global d8_1
    global pv2
    global ca2
    global pv2dep

    print "Le guerrier 1 fait son jet de toucher."
    print str(d20_1) + "!"
    if d20_1 >= ca2:
        print "Touché!"
        pv2 = pv2 - d8_1
        print str(d8_1) + "points de dégât!"
        print "Le guerrier 2 est à " + str(pv2) + "/" + str(pv2dep) + " PV!"
    else:
        print "Raté!"

def attaque2():
    global d20_2
    global d8_2
    global pv1
    global ca1
    global pv1dep

    print "Le guerrier 2 fait son jet de toucher."
    print str(d20_2) + "!"
    if d20_2 >= ca1:
        print "Touché!"
        pv1 = pv1 - d8_2
        print str(d8_2) + "points de dégât!"
        print "Le guerrier 1 est à " + str(pv1) + "/" + str(pv1dep) + " PV!"
    else:
        print "Raté!"

def resultat():
    global cnt

    print "Le combat prend fin au round " + str(cnt) + "."
    print 


intro()
nouveauCombat()
joaquin
  • 82,968
  • 29
  • 138
  • 152
erlang
  • 27
  • 1
  • 3

2 Answers2

2

attaque1() and attaque2() are called from calcInitiative() so if it doesn't get called they won't either.

Your while loop executes while ((pv1 <= 0) or (pv2 <= 0))

but you've defined them to be

pv1 = random.randint(1,10)
pv1 = (pv1) # this line does nothing
pv2 = random.randint(1,10)
pv2 = str(pv2)

So neither can ever be <= 0 so the while loop will never be entered, and calcInitiative() will never be called.

You're writing your Python code like it were BASIC. You should go through the Python tutorial, and probably a general tutorial on object oriented programming, to learn about things like classes.

A good test for yourself is you should be able to write that program without using globals at all.

agf
  • 171,228
  • 44
  • 289
  • 238
  • Thanks for the comments agf. Taking into account your comments, I was able to solve the problem. By the way, I have bookmarked the the link to the Python tutorial. – erlang Aug 09 '11 at 16:07
  • Please click the check mark next to my answer to accept it if it answered your question; you should do that for your previous question too. I think the resources you mentioned in your comment on the other question are all good, keep using them. – agf Aug 09 '11 at 16:09
1

BASIC and Python are not built on the same principles. This is a bit like trying to go from using a parachute to flying an airplane. Work your way though a good Python tutorial like Learn Python the Hard Way and it will clear up a bunch of your confusions and questions.

Jonathanb
  • 1,224
  • 1
  • 11
  • 16
  • Hi Jonathanb. I have read Bytes of Python (http://www.ibiblio.org/swaroopch/byteofpython/read/), I'm at The New Boston tutorial # 28 (http://www.youtube.com/watch?v=y_2uy1TOH1M&NR=1&feature=fvwp) and I was recently recommended to read Learn Python the hard way (which I do plan to read) by someone else, but at the moment I'm reading Invent in Python (inventwithpython.com/). So if you have any advice for me concerning my question, I'll be more than happy to read them. – erlang Aug 09 '11 at 15:39
  • Well, specifically I believe you'll find it easier to do this particular program if you use classes. Create a player class which knows how to roll it's initiative, roll an attack on a target, and take damage. Then the function call that actually executes your code just takes two characters, rolls initiative, and calls the two attack functions repeatedly, rolling initiative between every round. – Jonathanb Aug 09 '11 at 22:28