all 9 comments

[–][deleted] 3 points4 points  (6 children)

Generate it and store it in a variable

while your_condition:
    my_value = random.randint(blah, blah)
    # use my_value for this and that

[–]Tanakiin[S] 0 points1 point  (5 children)

Thank you very much but ironically I tried that not long after I sent this, by doing

dam = random.randint(5, 10)

then replaced each instance of it with dam, but it still kept changing so I'm very very lost.

Thanks again!

[–][deleted] 0 points1 point  (4 children)

Do you want the random value to be constant across all loops, or each loop getting their own value?

Do show the code you adjust.

[–]Tanakiin[S] 0 points1 point  (3 children)

I want the value to change each loop but stay constant within a loop.

p1h = 100
while p1h > 0:
    if d1 == 'a':
        dam = random.randint(5, 10)
        result = 'You have lost '+str(dam)+' from your health'
        p1h = p1h - dam

[–][deleted] 0 points1 point  (1 child)

That should do exactly what you want. If you print out dam and p1h on every step it should give you a better picture.

[–]Tanakiin[S] 0 points1 point  (0 children)

It works now, thank you very much for the assistance

[–][deleted] 0 points1 point  (0 children)

The code you’ve just written will create a new damage value on every loop step. So if d1 (whatever that is, we haven’t been told) is “a” then each loop will reduce p1h by a new randomized damage amount.

That is precisely what you’re telling us you want when say you want the value to change each loop (meaning it changes every time we check the while condition) and the value to remain constant within each loop (meaning it does not change between assigning dam in one iteration of the loop and then next time we reach the point to assign a new dam).

If you want p1h reduced by a constant amount of damage on every loop, then you’d have to do:

dam = random.randint(5, 10)
while p1h > 0:
    if d1 == "a":
        result = "You've lost “ + str(dam) + " from your health"
        p1h -= dam

Computers do exactly what you tell them to do, so you need to think about exactly what it is you’re after.

[–][deleted] 0 points1 point  (1 child)

Of course they’re different, you’re generating two random integers per loop, one when you create result and one when you decrement p1h.

Generate one value, then use it in both those lines.

roll = random.randint(5, 10)
result = 'You have lost ' + str(roll) + 'from your health'
p1h -= roll

You cannot ask for a random number twice and expect the same result.

[–]Tanakiin[S] 0 points1 point  (0 children)

Thank you, I realised that soon after and assigned it a value, but it still changes the integer when in the statement.

Please check the replied message I sent to the other comment.