Question

In: Computer Science

" line 48"     return delta.days     ^ SyntaxError: 'return' outside function import datetime def nowDate():...

" line 48"
    return delta.days
    ^
SyntaxError: 'return' outside function

import datetime


def nowDate():
    current_time = datetime.datetime.now()
    new_time = datetime.datetime(
        current_time.year,
        current_time.month,
        current_time.day,
        0,
        0,
        0,
        )
    return new_time


def plusOneDay(daytime):
    tomorrow = daytime + datetime.timedelta(days=1)
    return tomorrow


def nDaystoHoliday():
    NJCU_holidays = [
        (datetime.date(2019, 1, 1), "New Year's Day"),
        (datetime.date(2019, 1, 21), 'Martin Luther King, Jr. Day'),
        (datetime.date(2019, 2, 19), "President's Day"),
        (datetime.date(2019, 4, 19), 'Good Friday'),
        (datetime.date(2019, 5, 27), 'Memorial Day'),
        (datetime.date(2019, 7, 4), 'Independence Day'),
        (datetime.date(2019, 9, 2), 'Labor Day'),
        (datetime.date(2019, 10, 14), 'Columbus Day'),
        (datetime.date(2019, 11, 11), "Veteran's Day"),
        (datetime.date(2019, 11, 28), 'Thanksgiving'),
        (datetime.date(2019, 12, 25), 'Christmas Day'),
        ]


now = datetime.datetime.now().date()
print (now)
date = NJCU_holidays[0][0]
print (date)
for i in range(1, len(NJCU_holidays)):
    holiday = NJCU_holidays[i][0]
    delta = holiday - now
    if(delta.days > 0):
      return delta.days

time = nowDate()
plusOneDay(time)
nDaystoHoliday()

Solutions

Expert Solution

import datetime


def nowDate():
current_time = datetime.datetime.now()
new_time = datetime.datetime(
current_time.year,
current_time.month,
current_time.day,
0,
0,
0,
)
return new_time


def plusOneDay(daytime):
tomorrow = daytime + datetime.timedelta(days=1)
return tomorrow


def nDaystoHoliday():
   NJCU_holidays = [
       (datetime.date(2019, 1, 1), "New Year's Day"),
       (datetime.date(2019, 1, 21), 'Martin Luther King, Jr. Day'),
       (datetime.date(2019, 2, 19), "President's Day"),
       (datetime.date(2019, 4, 19), 'Good Friday'),
       (datetime.date(2019, 5, 27), 'Memorial Day'),
       (datetime.date(2019, 7, 4), 'Independence Day'),
       (datetime.date(2019, 9, 2), 'Labor Day'),
       (datetime.date(2019, 10, 14), 'Columbus Day'),
       (datetime.date(2019, 11, 11), "Veteran's Day"),
       (datetime.date(2019, 11, 28), 'Thanksgiving'),
       (datetime.date(2019, 12, 25), 'Christmas Day'),
       ]
   now = datetime.datetime.now().date() #we have to give tab space to each line and arrange them inside the function.
   print (now)
   date = NJCU_holidays[0][0]
   print (date)
   for i in range(1, len(NJCU_holidays)):
       holiday = NJCU_holidays[i][0]
       delta = holiday - now
       if(delta.days > 0):
       return delta.days #return statement works only in a function.

time = nowDate()
plusOneDay(time)
nDaystoHoliday()

There is no error in the code except that you have to give indentation (tab space) from the now = datetime.datetime.now().date() to the return statement. So that it will be inside the function.

If you have any doubt comment me.


Related Solutions

#Python 3.7 "Has no attribute" error - def get():     import datetime     d = date_time_obj.date()...
#Python 3.7 "Has no attribute" error - def get():     import datetime     d = date_time_obj.date()     return(d) print(a["Date"]) print("3/14/2012".get()) How to write the "get" function (without any imput), to convery the format ""3/14/2012" to the format "2012-03-14", by simply using "3/14/2012".get() ?
What does each line of this python code do? import datetime import getpass print("\n\nFinished execution at...
What does each line of this python code do? import datetime import getpass print("\n\nFinished execution at ", datetime.datetime.now()) print(getpass.getuser())
import random # define functions def rollDice(): # function returns a random number between 1 and...
import random # define functions def rollDice(): # function returns a random number between 1 and 6 def userWon(t1, t2): # function accepts player total and computer total # function returns true if player wins # function returns false if computer wins def main(): # each player rolls two Dice player = rollDice() + rollDice() computer = rollDice() + rollDice() # ask the player if they want to roll again again = int(input("Please enter 1 to roll again. Enter 2...
Write a Python function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the...
Write a Python function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the given wordlist  An anagram dictionary has each word with the letters sorted alphabetically creating a “key”.
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return...
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return the length of the longest chain of 1's that start from the beginning. You MUST use a while loop for this! We will check. >>> longest_chain([1, 1, 0]) 2 >>> longest_chain([0, 1, 1]) 0 >>> longest_chain([1, 0, 1]) 1 """ i = 0 a = [] while i < len(submatrix) and submatrix[i] != 0: a.append(submatrix[i]) i += 1 return sum(a) def largest_rectangle_at_position(matrix: List[List[int]], x:...
############callbacks ##def function_1( x ) : return x ** 2 ##def function_2( x ) : return...
############callbacks ##def function_1( x ) : return x ** 2 ##def function_2( x ) : return x ** 3 ##def function_3( x ) : return x ** 4 ## ###### create a list of callbacks to each of the functions ######by referencing their names ## ##callbacks = [ function_1 , function_2 , function_3 ] ## ######display a heading and the result of passing a value to each of the ######named functions: ## ##print( '\nNamed Functions:' ) ##for function in callbacks...
from PIL import Image def stringToBits(string):     return str(bin(int.from_bytes(string.encode(), 'big')))[2:] def bitsToString(bits):     if bits[0:2] != '0b':         bits.
from PIL import Image def stringToBits(string):     return str(bin(int.from_bytes(string.encode(), 'big')))[2:] def bitsToString(bits):     if bits[0:2] != '0b':         bits = '0b' + bits     value = int(bits, 2)     return value.to_bytes((value.bit_length() + 7) // 8, 'big').decode() def writeMessageToRedChannel(file, message):     image = Image.open(file)     width, height = image.size     messageBits = stringToBits(message)     messageBitCounter = 0     y = 0     while y < height:         x = 0         while x < width:             r, g, b, a = image.getpixel((x, y))             print("writeMessageToRedChannel: Reading pixel %d, %d - Original values (%d, %d, %d, %d)"...
In Python write a function with prototype “def dictsort(d):” which will return a list of key-value...
In Python write a function with prototype “def dictsort(d):” which will return a list of key-value pairs of the dictionary as tuples (key, value), reverse sorted by value (highest first) and where multiple keys with the same value appear alphabetically (lowest first).
import random #the menu function def menu(list, question): for entry in list: print(1 + list.index(entry),end="") print(")...
import random #the menu function def menu(list, question): for entry in list: print(1 + list.index(entry),end="") print(") " + entry) return int(input(question)) plz explain this code
import random import turtle def isInScreen(win,turt): leftBound = -win.window_width() / 2 rightBound = win.window_width() / 2...
import random import turtle def isInScreen(win,turt): leftBound = -win.window_width() / 2 rightBound = win.window_width() / 2 topBound = win.window_height() / 2 bottomBound = -win.window_height() / 2 turtleX = turt.xcor() turtleY = turt.ycor() stillIn = True if turtleX > rightBound or turtleX < leftBound: stillIn = False if turtleY > topBound or turtleY < bottomBound: stillIn = False return stillIn def main(): wn = turtle.Screen() # Define your turtles here june = turtle.Turtle() june.shape('turtle') while isInScreen(wn,june): coin = random.randrange(0, 2) if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT