Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6243515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:09:13+00:00 2026-05-24T12:09:13+00:00

In a previous post, I was told I am coding in Python like you

  • 0

In a previous post, I was told I am coding in Python like you would in BASIC (I am not “thinking” in Python yet). This is probably true and this is why I need help. This being said, I have read one book about Python so far (Bytes of Python), I bought two more books online (one of which is Invent with Python, which I am reading) and I did many tutorials from The New Boston. Still, my question might sound newbie-ish to you.

In FreeBasic, variables are all globalized. In Python, I have to globalize them for every single new function I create. This isn’t very practical. So what am I expected to do? How would a “native Python speaker” tackle this problem? Below you’ll find a small program I have created where I globalize in all my variables inside functions.

#-*- coding: iso8859_1 -*-

import random

ca1 = 10
ca2 = 10
taco = 20
pv1 = random.randint(1,10)
pv2 = random.randint(1,10)
cnt = 0
pv1Dep = pv1
pv2Dep = 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 taco
    global pv1
    global pv2
    global cnt
    global pv1Dep
    global pv2Dep
    global d20_1
    global d8_1
    global d20_2
    global d8_2


    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 ca1
    global ca2
    global taco
    global pv1
    global pv2
    global cnt
    global pv1Dep
    global pv2Dep
    global d20_1
    global d8_1
    global d20_2
    global d8_2

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

def calcInitiative():
    global ca1
    global ca2
    global taco
    global pv1
    global pv2
    global cnt
    global pv1Dep
    global pv2Dep
    global d20_1
    global d8_1
    global d20_2
    global d8_2

    initiative1 = random.randint(1,10)
    initiative2 = random.randint(1,10)
    print "Le guerrier 1 fait son jet d'initiative."
    print str(initiative1) + "!"
    print
    print "Le guerrier 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 + "ROUND " + str(cnt) + ": ATTAQUE" + ast
        print
        attaque1()
        print
        attaque2()
    elif initiative1 < initiative2:
        print "Le guerrier 1 attaque en premier."
        print
        print ast + "ROUND " + str(cnt) + ": ATTAQUE" + ast
        print
        attaque1()
        print
        if pv2 > 0:
            print
            attaque2()
    else:
        print "Le guerrier 2 attaque en premier."
        print
        print ast + "ROUND " + str(cnt) + ": ATTAQUE" + ast
        print
        attaque2()
        print
        if pv1 > 0:
            print
            attaque2()

def attaque1():
    global ca1
    global ca2
    global taco
    global pv1
    global pv2
    global cnt
    global pv1Dep
    global pv2Dep
    global d20_1
    global d8_1
    global d20_2
    global d8_2

    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 ca1
    global ca2
    global taco
    global pv1
    global pv2
    global cnt
    global pv1Dep
    global pv2Dep
    global d20_1
    global d8_1
    global d20_2
    global d8_2

    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 ca1
    global ca2
    global taco
    global pv1
    global pv2
    global cnt
    global pv1Dep
    global pv2Dep
    global d20_1
    global d8_1
    global d20_2
    global d8_2

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

    if pv1 == pv1Dep:
        print "Le guerrier 1 n'a pas la moindre égratignure."
    elif pv1 > 0:
        print "Le guerrier 1 est blessé."
    else:
        print "Le guerrier 1 est mort."

    print "Il finit le combat avec " + str(pv1) + "/" +str (pv1Dep) + " PV."
    print

    if pv2 == pv2Dep:
        print "Le guerrier 2 n'a pas la moindre égratignure."
    elif pv2 > 0:
        print "Le guerrier 2 est blessé."
    else:
        print "Le guerrier 2 est mort."

    print "Il finit le combat avec " + str(pv2) + "/" +str (pv2Dep) + " PV."
    print

intro()
nouveauCombat()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:09:14+00:00Added an answer on May 24, 2026 at 12:09 pm

    I’ve reorganized your program to show you the basics of object oriented programming. You need to learn about classes and objects. You should also look at string formatting for the right way to put strings together.

    Basically, you create two player objects, and then create a combat object for those two players. Then, you call methods on the combat object in order to actually do the combat. self is how an instance of a class refers to itself. Any good Python tutorial should teach you about all of these things.

    The only remaining global variable is a constant, ast. Global constants can occasionally be OK.

    #-*- coding: iso8859_1 -*-
    import random
    ast = "*" * 7
    
    class Player(object):
        def __init__(self, num, ca):
            self.ca = ca
            self.num = num
            self.pv = random.randint(1,10)
            self.d20 = random.randint(1,20)
            self.d8 = random.randint(1,8)
            self.pvDep= self.pv
    
    class Combat(object):
        def __init__(self, player1, player2):
            self.player1 = player1
            self.player2 = player2
            self.cnt = 0
    
        def intro(self):
            print "Imaginez deux guerriers de D&D 2e édition qui se battent."
            print
            print "Guerrier 1: " + str(player1.pv) + " PV, épée longue (1-8 points de dégât), TACO de 20, CA de " + str(player1.ca) + "."
            print "Guerrier 2: " + str(player2.pv) + " PV, épée longue (1-8 points de dégât), TACO de 20, CA de " + str(player2.ca) + "."
            print
    
        def nouveauCombat(self):
            print ast + "NOUVEAU COMBAT" + ast
            print
            while ((self.player1.pv > 0) and (self.player2.pv > 0)):
                self.cnt = self.cnt + 1
                print ast + "ROUND " + str(self.cnt) + ": INITIATIVE" + ast
                print
                self.calcInitiative()
                print
            print ast + "RESULTAT" + ast
            print
            self.resultat()
    
        def calcInitiative(self):
            initiative1 = random.randint(1,10)
            initiative2 = random.randint(1,10)
            print "Le guerrier 1 fait son jet d'initiative."
            print str(initiative1) + "!"
            print
            print "Le guerrier 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 + "ROUND " + str(self.cnt) + ": ATTAQUE" + ast
                print
                self.attaque(self.player1, self.player2)
                print
                self.attaque(self.player2, self.player1)
            elif initiative1 < initiative2:
                print "Le guerrier 1 attaque en premier."
                print
                print ast + "ROUND " + str(self.cnt) + ": ATTAQUE" + ast
                print
                self.attaque(self.player1, self.player2)
                print
                if self.player2.pv > 0:
                    print
                    self.attaque(self.player2, self.player1)
            else:
                print "Le guerrier 2 attaque en premier."
                print
                print ast + "ROUND " + str(self.cnt) + ": ATTAQUE" + ast
                print
                self.attaque(self.player2, self.player1)
                print
                if self.player1.pv > 0:
                    print
                    self.attaque(self.player1, self.player2)
    
        def attaque(self, player1, player2):
            print "Le guerrier" + str(player1.num) + " fait son jet de toucher."
            print str(player1.d20) + "!"
            if player1.d20 >= player2.ca:
                print "Touché!"
                player2.pv = player2.pv - player1.d8
                print str(player1.d8) + " points de dégât!"
                print "Le guerrier 2 est à " + str(player2.pv) + "/" + str(player2.pvDep) + " PV!"
            else:
                print "Raté!"
    
        def resultat(self):
            print "Le combat prend fin au round " + str(self.cnt) + "."
            print
    
            if player1.pv == player1.pvDep:
                print "Le guerrier 1 n'a pas la moindre égratignure."
            elif player1.pv > 0:
                print "Le guerrier 1 est blessé."
            else:
                print "Le guerrier 1 est mort."
    
            print "Il finit le combat avec " + str(player1.pv) + "/" +str (player1.pvDep) + " PV."
            print
    
            if player2.pv == player2.pvDep:
                print "Le guerrier 2 n'a pas la moindre égratignure."
            elif player2.pv > 0:
                print "Le guerrier 2 est blessé."
            else:
                print "Le guerrier 2 est mort."
    
            print "Il finit le combat avec " + str(player2.pv) + "/" +str (player2.pvDep) + " PV."
            print
    
    
    player1 = Player(1, 10)
    player2 = Player(2, 10)
    combat = Combat(player1, player2)
    combat.intro()
    combat.nouveauCombat()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a previous post detailing a CI setup for Python. The asker and
I looked at previous post based on this but they do not relate. I
That's a question really similar to this previous post of mine. I need to
Ok after my previous post, i have made some progress, yet still i need
This relates to my previous post: jQuery .load Method causing page refresh AJAX I
This question is related to a previous post of mine Here . Basically, I
This is a continuation of a previous post: TPanel color is black no matter
This post is related to my Previous Post . There I was creating the
This post is the follow-up of my previous post (Create and retrieve object list
In a previous post ( Don't show this if the url contains the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.