Python Forum
Trying to Repeat a Function?
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to Repeat a Function?
#1
So I'm quite new to Python and I'm trying to make projects to hone my skills and learn new ones. Currently, I am making a game where your opponent (AI) randomly chooses an action (Attack/Defense) and if they attack, then I want it to choose a random number (Say from 1-10) and store it in a variable. I'm quite confused on how I would go about doing this, mainly how I would repeat a section of the code so when the AI turn is over, it is then the players turn and repeat. I'm also confused on how I would generate a random integer as the other methods I have found online don't seem to work for me. Again, I'm only a beginner and still trying to learn :)
Reply
#2
first import random. then use "random.randint(1, 10)" to generate a number 1 - 10. you can repeat in different ways. I'll show you down below

You can use range
for x in range(1, 10):   #Will happen 10 times
    if Variable == 0:   #0 is attack
        num = random.randint(1, 10)
    elif Variable == 1:   #1 is defense
        pass   #Do stuff
You can use while
while defeat == False:
    if Variable == 0:   #0 is attack
        num = random.randint(1, 10)
    elif Variable == 1:   #1 is defense
        pass   #Do stuff
You can have a variable for keeping track of who's turn it is and a function that will trigger another function based on who's turn is is. If it's the player's turn it will trigger this function. If not it will trigger this function. Hope this solves your problem. Also remember to import random
Edit - I just realized you want a function repeated. In that case just execute the function in the lines set to repeat
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,258 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Repeat request by else stsxbel 2 1,150 Jul-30-2022, 03:34 PM
Last Post: stsxbel
  get out of while loop and stop repeat Frankduc 11 2,866 Apr-26-2022, 10:09 PM
Last Post: deanhystad
  Avoid multiple repeat in indent Frankduc 8 2,790 Jan-18-2022, 05:46 PM
Last Post: Frankduc
  How to discard list repeat values akanowhere 7 3,576 Dec-28-2020, 09:14 PM
Last Post: akanowhere
  I want to check if the input is str or is int & if it's str repeat the loop HLD202 4 2,721 Nov-23-2020, 11:01 PM
Last Post: perfringo
  is there a way: repeat key word args Skaperen 2 2,200 Feb-03-2020, 06:03 PM
Last Post: Skaperen
  Python Script to repeat Photoshop action in folders and subfolders silfer 2 4,506 Jul-25-2019, 03:12 PM
Last Post: silfer
  While loop repeat Runner83 5 4,200 Nov-11-2018, 10:50 AM
Last Post: MasterJediKnight7
  Regular expressions help re.error: multiple repeat at position 23 JoseSalazar1 2 6,576 Sep-18-2018, 01:29 AM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020