0

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")
  • @imjoseangel the code isn't working, when I run it I get a syntax error saying: line 4 if (hour >= startDay and <= endDay); ^^ SyntaxError: invalid syntax – Katy Armstrong Nov 05 '22 at 08:39
  • Dear @katy-armstrong what is your question? Think that `guard` variable is already True so something like `if 7 < hour < 17:` would do the trick. You only need to return `guard = False` if your code. Look for other examples on Stackoverflow -> https://stackoverflow.com/a/13628825/6477655 – imjoseangel Nov 05 '22 at 08:41

4 Answers4

0

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

Aarav
  • 41
  • 3
0
 if (hour >= startDay and hour <= endDay):
    return True
else:
    return False 
Alexey
  • 36
  • 3
0

Your code was giving you syntax error cause you forgot to add == (comparison operator) and after the or you forgot to add your hour variable and do define hour and guard variable

if hour <= 7 or hour >= 17: guard == True else: guard == False

if guard == True: print("You're in!") else: print("The gate is open")

  • I am getting this syntax error: if hour >= 7 or <= 17: ^^ SyntaxError: invalid syntax (It is stating <= is incorrect but I'm not sure why) – Katy Armstrong Nov 05 '22 at 09:05
0

Your end answer is incorrect. The guard doesn't need to be there between 7-17, since the gate is open. The reason the guard needs to be there is to open the gate. Between 17.0000001-6.99999999, the gate is locked and you would need a guard there to open the gate.

It states there is a guard at 4, but the question fails to specifcy other times you'd need to "schedule" the guards availability in your coding.

Unfortunately, I'm as new as they'd come so I have nothing to contribute in terms of helping with the correction of the code, but I'm working on the same question right now and thought I'd toss my hat in with what needs to be resolved.

Mike
  • 1
  • Of course, I could be making things more complicated than needed. Looking over the code, it seems you posted a guard anytime the gate is locked – Mike May 29 '23 at 06:59