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: ")
CodePudding user response:
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
CodePudding user response:
There's a lot of mistakes there, on your first code, you forgot :
after if and else, then inside the if statement, you forgot the hour
variable that compares to 17, then your guard variable assignment inside the if and else block should use =
instead of ==
, finally on the last if statement, you should use the comparison ==
instead of assignment =
, like so:
if hour <= 7 or hour >= 17:
guard = True
else:
guard = False
if guard == True:
print("You're in!")
else:
print("The gate is open")
CodePudding user response:
if (hour >= startDay and hour <= endDay):
return True
else:
return False
CodePudding user response:
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")