Skip to content

Instantly share code, notes, and snippets.

@DuffleOne
Last active October 4, 2017 09:59
Show Gist options
  • Save DuffleOne/695ca9a91a0df6cff1a18e44478ed168 to your computer and use it in GitHub Desktop.
Save DuffleOne/695ca9a91a0df6cff1a18e44478ed168 to your computer and use it in GitHub Desktop.
# Let's make our own "int" function.
# This is because int() can cause the program to crash
def makeInt(strVal):
try: # TRY and execute the following code, be aware it may crash though
return int(strVal) # If it didn't crash, just return the string value as an Integer
except ValueError: # However, if it did crash, AND It crashed because the Value can't be turned into an Integer
return None # Don't actually crash, just return "None"
# "None" is a special key word in Python, it literally means nothing.
# "" is nothing by human standards, but to python, "" is a valid string like any other, it's just empty
# So "None" literally means nothing by Python standards. Other langauges refer to it as "null"
# If you follow the idea that "main()" is your main program, you should have no logic outside of it.
# So the "print()" statement at the beginning has been moved inside it.
def main():
print("Welcome, let's find out if you are due overtime for your work")
NoCount = 0 # Initiate the value
UserName = "" # Initate the value
HoursWorked = None # Initate the value
# We don't want to duplicate the logic
# So instead of writing Username = str(input()) etc etc, we just declare it as null, and let the loop handle it
# This means this loop should always run at least once, but can run for many cycles (also known as iterations)
while (UserName == ""): # While the user entered nothing
UserName = str(input("Hello, can I have your name please?: ")) # Ask for their name
if (UserName.upper() == "NO" or UserName.upper() == "N"): # If they enter "N" or "No"
# You may think, why Upper()? It makes comparison easy, now the user can enter:
# No
# nO
# no
# NO
# And we don't need a complex comparison function
NoCount += 1 # They've said no (again)
if (NoCount >= 3): # If they've said No 3 times:
print("Fine. I'll just leave it.") # Message that they won
return; # Quit the program
else: # Otherwise, if they've said No less than 3 times
print("That's quite rude, I really do need your name though.") # Message out
UserName = "" # Set the username back to "" - so the loop restarts and asks again.
# At this stage, we have their name.
while (HoursWorked is None): # Same principle as above but for HoursWorked
HoursWorked = makeInt(input("Please tell me how many hours you have worked: ")) # ask for the value
# This also turns it into an intger without failing
if (HoursWorked is None): # If they didn't enter anything, or we couldn't turn it into an Integer
print("Huh, didn't quite catch that, can you enter a number?") # Let them know
# Loop will auto cycle until a number is given
if (HoursWorked > 40): # Simple IF statement
print("%s, I am pleased to tell you that you are due overtime" % UserName) # String templating!
else:
print("I'm sorry %s, You are not due overtime." % UserName)
main() # Run main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment