-2

I'm making a simple python survival game and I want one day to pass every five turns (so every time x hits a multiple of 5, y = 1 + 1). Is there a simple way to do this?

1 Answers1

0

Try below:

y=0
for x in range(50):
    if x > 0 and x % 5 == 0:
        y+=1
    print ('x', x)
    print ('y', y)

Explanation:

  • for x in range(50): Will iterate value of x from 0 to 49
  • if x > 0 and x % 5 == 0: Checks if x is greater than 0 (you can remove this), and checks if x modulo 5 is zero
  • y += 1: Will increment value of y by 1
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
Homer
  • 424
  • 3
  • 7
  • `for x in range(50)` does not _print_ anything. `if x > 0` is unnecessary, just start `range` at 1. – Pranav Hosangadi Sep 14 '20 at 18:56
  • I am printing x in the second last line. x > 0 is totally optional, depending on user's requirement. – Homer Sep 14 '20 at 19:00
  • 1. Right. Your explanation says "for x in range(50): --> Will print value of x from 0 to 49". `for ...` _iterates_ over those values, it doesn't print anything. 2. Having an `if` in every iteration to filter out something that could be done by just changing the range of iterations instead is not efficient code. – Pranav Hosangadi Sep 14 '20 at 19:05