Search code examples
pythonif-statementconditional-statementssyntax-error

Checking conditionals Python


Hi I am stuck on a question. I am new to coding and in order to attend a bootcamp I need to answer some questions. Nothing I write works, I always get a syntax error.

This is the question:

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!'

Make use of the if statement structure to implement the program.

My original code was:

hour = (input("Please enter the hour: "))
if hour => 7 or =< 17
 return guard = True
else:
 return guard = False

if guard = True
 print("You're in!")
elif guard = false
 print(Gate is open)
else:
 print("Oops incorrect input, please enter a number: ")

Thanks for all of your help, I got it in the end. The correct code was:

The correct code was:

hour = int(input("Please enter the hour: "))
if (hour >= 7 and hour <= 17):
 guard = True
else:
 guard = False

if guard == True:
 print("You're in!")
elif guard == False:
 print("Gate is open")

Solution

  • Original Code:

    The first error as u stated is in the following line:

    if hour <= 7 or >= 17
    

    the corrected version would be:

    if hour <= 7 or hour >= 17:
    

    Secondly, in python '#' instead of '//' is used to comment. I am pretty sure that '//' is used for floor division.

    Thirdly, the operator '==' is used to check whether something two values are equal. The operator '=' should be used to assign your boolean values instead.

    You also have missing semicolons on some of your else statements so make sure to include them as well.

    Good Luck learning python