How do i loop a code until a certain number is created?

2024/11/19 0:26:17

This task is to determine the difference between two attributes, strength and skill, from game characters. The process for this is:

  • Determining the difference between the strength attributes.
  • The difference is divided by five and rounded down to create a ‘strength modifier’.
  • The same process is carried out for the skill attribute and named ‘skill modifier’.
  • Then the game begins:
    • Both players throw a six sided die.
    • If the values are the same, no changes happen.
    • If the values are different, the player with the highest value then gains the strength and skill worked out earlier and it is then added to their total. The player who has the lowest value has the strength and skill worked out taken away from their total.

This must repeat until the strength attribute is equal or below 0, however, this part I cant do, I know how to loop but this is always done by asking the user if they wish to go again.

c1sc=random.randint(1,6)
c2sc=random.randint(1,6)
if c1sc > c2sc:c1st=c1st+strengthmodifierc2st=c2st-strengthmodifierc1sk=c1sk+skillmodifierc2sk=c2sk-skillmodifierprint('Character 1, your strength attribute is: '+(str(c1st))+' and your skill attribute is: '+(str(c1sk)))print('Character 2, your strength attribute is: '+(str(c2st))+' and your skill attribute is: '+(str(c2sk)))
elif c2sc > c1sc:c1st=c1st-strengthmodifierc2st=c2st+strengthmodifierc1sk=c1sk-skillmodifierc2sk=c2sk+skillmodifierprint('Character 1, your strength attribute is: '+(str(c1st))+' and your skill attribute is: '+(str(c1sk)))print('Character 2, your strength attribute is: '+(str(c2st))+' and your skill attribute is: '+(str(c2sk)))
else:print('It\'s a draw, no changes were made.')
if c1sk < 0:c1sk=0
elif c2sk < 0:c2sk=0if c1st < 0:print('Character 1 died. Game over.')
elif c2st < 0:print('Character 2 died. Game over.')

Please help!

Answer

Let me introduce you to the while loop. You use this when you want something to occur if the specified condition is true or false.

I briefly edited your code.

import random

def main():player1=20player2=12strength=(20-10)/5strength_mod=int(strength)while player1>0 and player2>0:dice1=random.randint(1,6)dice2=random.randint(1,6)if dice1>dice2:player1+=strength_modplayer2-=strength_modprint("Player 1 strength is:  ", player1)print("Player 2 strength is:  ", player2)elif dice1<dice2:player2+=strength_modplayer1-=strength_modprint("Player 1 strength is:  ", player1)print("Player 2 strength is:  ", player2)main()

I gave players 1 and 2 initial values bet these can be changed to your liking. To round down, I made another variable and changed strength to an integer.

https://en.xdnf.cn/q/118609.html

Related Q&A

Finding the longest list in given list that contains only positive numbers in Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed l…

How to create multiple VideoCapture Objects

I wanted to create multiple VideoCapture Objects for stitching video from multiple cameras to a single video mashup.for example: I have path for three videos that I wanted to be read using Video Captur…

How to read Data from Url in python using Pandas?

I am trying to read the text data from the Url mentioned in the code. But it throws an error:ParserError: Error tokenizing data. C error: Expected 1 fields in line 4, saw 2url="https://cdn.upgrad.…

Testing multiple string in conditions in list comprehension [duplicate]

This question already has answers here:How to test multiple variables for equality against a single value?(31 answers)Closed 6 years ago.I am trying to add multiple or clauses to a python if statement…

Filter range from two dates in the same query Django/Python

I need the result from a query that filters two dates from the same model. I need to get in the result 5 days (today plus 4 days) from original date and sale from target date (today plus 4 more days) b…

Python While/For loop

how can I make this into a while loop and output the same thing????for x in range(56,120) :if (x < 57) :summation = 0summation = x + summationif (x == 119) :print (“Sum of integers from 56 to 1…

Read a file into a nested dictionary?

Say I have a simple file like so holding arbitrary values:A, 20, Monday, 14, Tuesday, 15, Tuesday, 16 B, 40, Wednesday, 14, Friday, 12How would I get it into a nested dictionary so that each k/v pair l…

Using .replace function

I have a code with more than 2500 lines that contains several references to GIS layers. I need to replace these layers in the code for several web maps so I have to find a way to automate a find and re…

Python Turtle unit of measurement

When we instantiate a turtle object, we can draw a circle. I wonder about the radius parameter of the circle() method. import turtle myTurtle = turtle.Turtle() myTurtle.circle(50)What is the unit of me…

Python reverse() vs [::-1] slice performance [duplicate]

This question already has answers here:Difference between reverse and [::-1](2 answers)Time complexity of reversed() in Python 3(1 answer)Closed last year.Python provides two ways to reverse a list: Li…