0

I want to append the date2 from this function into

def date_register():
 print("Enter date of registration")
 year = int(input("Enter a year: "))
 month = int(input("Enter a month: "))
 day = int(input("Enter a day: "))
 date1 = datetime.date(year,month,day)
 date2 = date1 + timedelta(days = 140)
 print("Check out date:",date2)

this function and it came out date2 is not defined

def update_A(row): #to update the roomA
 if len(roomA[row]) < 2: #if roomA is less than 2
 name = input("Enter your name here: ")
 print(date_register())
 roomA[row].append((name,date2))
 print("Your room no. is {} at row {}".format(roomA[row].index((name,date2))+1,row))
 print(Continue()) 

Seeking for help thank you

asked Aug 16, 2017 at 15:22
2
  • My friend, you forgot to return the result of function date_register to make it available in update_a. Commented Aug 16, 2017 at 15:29
  • Could u provide the solution? Commented Aug 16, 2017 at 15:32

2 Answers 2

2

date2 is not defined because it is not within the scope of update_A Please read here for more information on scope.

You also seem to be confusing return and print

In update_A, you write print(date_register()) but date_register doesn't return anything to be printed.

print sends string representations to the console and can't be used for assignment. Instead use return which basically forces a function call to resolve to the value next to the return statement. For example:

def foo:
 return "bar"
print(foo())

when foo is called, it will resolve to "bar" which is then printed to the console. for more on the difference and usage of print() and return see here

To use date2 in update_A you should return it and assign it as follows:

def date_register():
 print("Enter date of registration")
 year = int(input("Enter a year: "))
 month = int(input("Enter a month: "))
 day = int(input("Enter a day: "))
 date1 = datetime.date(year,month,day)
 date2 = date1 + timedelta(days = 140)
 print("Check out date:",date2)
 return date2 
def update_A(row): #to update the roomA
 if len(roomA[row]) < 2: #if roomA is less than 2
 name = input("Enter your name here: ")
 date2 = date_register() #assign date2 returned value
 print(date2)
 roomA[row].append((name,date2))
 print("Your room no. is {} at row {}".format(roomA[row].index((name,date2))+1,row))
 print(Continue()) 
answered Aug 16, 2017 at 15:35
Sign up to request clarification or add additional context in comments.

Comments

1

I've corrected one or two other minor mistakes.

import datetime
def date_register():
 print("Enter date of registration")
 year = int(input("Enter a year: "))
 month = int(input("Enter a month: "))
 day = int(input("Enter a day: "))
 date1 = datetime.date(year,month,day)
 date2 = date1 + datetime.timedelta(days = 140)
 print("Check out date:",date2)
 return date2
def update_A(row): #to update the roomA
 if len(roomA[row]) < 2: #if roomA is less than 2
 name = input("Enter your name here: ")
 checkout_date = date_register()
 print(checkout_date)
 roomA[row].append((name,checkout_date))
 print("Your room no. is {} at row {}".format(roomA[row].index((name,checkout_date))+1,row))
roomA = {1: []}
update_A(1)

Here's the output.

Enter your name here: John
Enter date of registration
Enter a year: 1954
Enter a month: 7
Enter a day: 12
Check out date: 1954年11月29日
1954年11月29日
Your room no. is 1 at row 1

Apparently you need to work out how to print the check-out date.

answered Aug 16, 2017 at 15:45

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.