Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Josephus Survivors and Victims Game

Python Josephus Survivors and Victims Game

Document Object Reference Manual Python3 Examples

30 people are on a boat that is overloaded, and 15 people need to get off.

So the people line up in a queue, and their position in the queue is their number.

Count off starting from 1; the person who counts to 9 gets off the boat.

This repeats until only 15 people remain on the boat. Which numbered people got off the boat?

Example

people

=

{

}

for

x

in

range

(

1

,

31

)

:

people

[

x

]

=

1

print

(

people

)

check

=

0

i

=

1

j

=

0

while

i

<=

31

:

if

i

==

31

:

i

=

1

elif

j

==

15

:

break

else

:

if

people

[

i

]

==

0

:

i

+=

1

continue

else

:

check

+=

1

if

check

==

9

:

people

[

i

]

=

0

check

=

0

print

(

"

No. {} got off the boat

"

.

format

(

i

)

)

j

+=

1

else

:

i

+=

1

continue

Running the above example, the output is:

9号下船了

18号下船了

27号下船了

6号下船了

16号下船了

26号下船了

7号下船了

19号下船了

30号下船了

12号下船了

24号下船了

8号下船了

22号下船了

5号下船了

23号下船了

Document Object Reference Manual Python3 Examples