Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If statement [Guard List]
#1
hello everyone, im a  soldier and in my unit there is a lot of guard duty which means that everyday each soldier have a 3 hours guard duty which he takes care of an a specific area.
there is a specific guy which makes the list of the guards keeping time and what im trying to make is a program which make that list automatic without a man manualy making it.
there is 2 areas that sometimes your guard there sometimes there so here is what i made for now :

from random import *
guards = [] #here there is the names of all the available guards!
newguards = []
guardscount = input('How many guards guard?:')
for i in range(0,int(guardscount)):
   i = input('What is the guard name:?')
   guards.append(i)

guardians = {}
times = ['23:00-2:00','00:00-03:00','02:00-05:00','03:00-06:00','05:00-08:00','06:00-09:00','08:00:11:00','09:00-12:00',
         '11:00-14:00','12:00-15:00','14:00-17:00','15:00-18:00','17:00-20:00','18:00-21:00','20:00-23:00','21:00-00:00']
day = 21
month = 3
index = 15
while day < 23:
   for x in guards:
       guardians[x] = times[randint(0, index)]
       times.remove(guardians[x])
       index -= 1
       if index == 0 :
           print('Today', day, '/', month, 'This is the Guardians List:', guardians)
           times = ['23:00-2:00','00:00-03:00','02:00-05:00','03:00-06:00','05:00-08:00','06:00-09:00','08:00:11:00','09:00-12:00',
                        '11:00-14:00','12:00-15:00','14:00-17:00','15:00-18:00','17:00-20:00','18:00-21:00','20:00-23:00','21:00-00:00']
           index = 15
           day += 1

   if len(guards) == 0:
       guards = newguards
       newguards = []
       continue
the problem i have here is that somehow the if index =0 statment apply even when index is not 0 i dont know why..

sorry about my english...its not my home language so..

Moderator:
sparkz_alot: removed formating from code
Reply
#2
Please, repost the code without the formatting
Reply
#3
(Mar-22-2017, 01:54 PM)yuvalsaias Wrote: for i in range(0,int(guardscount)):
   i = input('What is the guard name:?')
   guards.append(i)
 

you are redefining what "i" is. what you probably want is :
for i in range(0, int(guardscount)):
   names = input('What is the guard name:?')
   guards.append(names)
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#4
Hello everyone.
i am a soldier and in my unit our job is to guard around the base 24/7.
now we have a hugh problem which its falling on one man to make a list that contain who guards where because there is 2 guarding towers and that specific man always make a list of who guards where and when because you have a minimum distance between one guard to job to another.
now what i am trying to make is a program which do it automatic.
i need your help to show me the way and bright my eyes on how to do it.
now there is a few condition for that program about the guards :
1)the distance btw a guard watch to the next guard watch needs to be around (amount of people * 3) of course its + -
the time of guard watch is 3 hours so thats way * 3.
2)if a guy did a night watch which there is 4 every day 2 at each guard watch, means that tomorrow he wont do a night watch and will doo one at the day or evening,means the program have to calculate who did last night a night watch and give him a hour not in the night.
the guard times are:(00:00-03:00,03:00-06:00,06:00-09:00,09:00-12,12-15,15-18,18-21,21-00.....)
now a friend told me to try doing it with recursive but didnt figured out how.
thank you for your help:)
Reply
#5
Had this exact same question not long ago in this subforum.  In fact it was posted by you on 22 Mar 2017.

Please do not post a new topic that is the same as the last one.  Just continue on with the original post. I am beginning to think this is actually a homework assignment.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#6
Quote:I am beginning to think this is actually a homework assignment.
You don't say?  I've never heard such a contrived story to frame something as not homework.
Reply
#7
this is not a homework assignment,its something im working on for a month and im stuck and cant figure out how to do it so im asking for help.
its something im working on for the situation im dealing in the army which made me think of a solution for that problem and thats what im trying to make
Reply
#8
I've merged the threads together. Those of us who help out, check the whole forum, so posting multiple times doesn't really help you at all.

With these sorts of problems, it can help to write out different situations that can happen, and then take a step back and see if any patterns exist. From there, writing the actual code is almost easy, it's the ideas and structure that you need to think about.

For example, you probably don't want to type in each guard's name every time you run the script. Do you want the schedule to remain roughly the same week to week, or do you want to vary it so the same people don't work the same shifts all the time? Either way, I think the first thing you should do is create a small database (sqlite and sqlalchemy make this very easy) to keep track of the guards, and another table to keep track of their shifts.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,662 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Change a list to integer so I can use IF statement buckssg 3 2,234 Sep-21-2021, 02:58 AM
Last Post: bowlofred
  How to invoke a function with return statement in list comprehension? maiya 4 2,829 Jul-17-2021, 04:30 PM
Last Post: maiya
  An IF statement with a List variable dedesssse 3 8,213 Jul-08-2021, 05:58 PM
Last Post: perfringo
  using element on a list as condition statement glennford49 11 4,966 May-21-2020, 12:53 PM
Last Post: hussainmujtaba
  Help: list comprehension for loop with double if statement mart79 3 2,423 May-04-2020, 06:34 AM
Last Post: buran
  Taking brackets out of list in print statement pythonprogrammer 3 2,395 Apr-13-2020, 12:25 PM
Last Post: perfringo
  printing a list contents without brackets in a print statement paracelx 1 2,123 Feb-15-2020, 02:15 AM
Last Post: Larz60+
  Checking for an item in a list (if then statement) Sailnir 1 2,054 Jul-18-2019, 05:36 PM
Last Post: ndc85430
  skeleton class needs working instantiation guard hereathome 3 3,028 Dec-10-2017, 03:35 PM
Last Post: hshivaraj

Forum Jump:

User Panel Messages

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