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
2 Answers 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())
Comments
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.
returnthe result of functiondate_registerto make it available inupdate_a.