Python Forum

Full Version: If statement question help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys,

New to this forum and to the programming language. I was hoping someone could help me out with he following task I have been given. Below is my program so far and the question is at the bottom.

guard = bool(input("True or false the guard is present on the gate"))
daytime = int(input ("enter the time of day"))



if (daytime >= 7) and (daytime <= 17):
  print ("You're in!")
  
  
if (daytime < 7) or (daytime > 17) and (guard == True):
  print("You're in!")

else:
  print ("you are not in")
  




I have the beginning of my program, but i am struggling with how to write the second variable on line 10 of my code.
When i set the guard variable to False it still prints "You're in' instead of "You're not in"



At your school, the front gate is locked at night for safety. You often need to study late on campus. There is sometimes a night guard on duty who can let you in. You want to be able to check if you can access the school campus at a particular time.

The current hour of the day is given in the range 0, 1, 2 … 23 and the guard’s presence is indicated by with a True/False boolean.

If the hour is from 7 to 17, you do not need the guard to be there as the gate is open
If the hour is before 7 or after 17, the guard must be there to let you in
Using predefined variables for the hour of the day and whether the guard is present or not, write an if statement to print out whether you can get in.

Example start:
hour = 4
guard = True

Example output:
'You're in!'
place the parenthesis around the complex condition like if (daytime < 7 or daytime > 17) and guard:

note: you don't need guard == True, as the condition will be true without.
print(bool("True"), bool("False"))
Output:
True True
guard will always be True if you type anything for input("True or false the guard is present on the gate")

Python lets you do this:
7 <= daytime <= 17
There is no need to ask if there is a guard when daytime is between 7 and 17. You should only ask the guard question when the guard needs to be on duty.
deanhystad Wrote:guard will always be True if you type anything for input("True or false the guard is present on the gate")
Correct. didn't pay attention to input statement.

Normally a boolean value is implicitly set:
guard = True
or
guard = False
Then can be used as conditional.
>>> def where_is_guard():
...     if guard:
...         print(f"guard is present")
...     else:
...         print("guard is away")
... 
>>> guard = True
>>> where_is_guard()
guard is present
>>> guard = False
>>> where_is_guard()
guard is away
>>>
Thought it might be an idea to randomise the guard and automate the student time!

import random

mylist = [True, False]
def guard():
    hay_guardia = random.choice(mylist)
    return hay_guardia

def getIn2(hour):
    print(f'It is now {hour}:00')
    g = guard()
    if (hour >= 17 or hour < 7) and g:        
        print('"I\'ll let you in." said the guard.')
    elif (hour >= 17 or hour < 7) and not g:
        print('"No one here to open the gate." said the intelligent door.')
    else:
        print('Welcome to this wonderful place of education!')

for i in range(0, 24):
    getIn2(i)