我想将此函数的date2追加到
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)
这个函数和它出来了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: ")
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())
寻求帮助谢谢
答案 0 :(得分:2)
date2
未定义,因为它不在update_A
的范围内
有关范围的更多信息,请阅读here。
您似乎也在混淆return
和print
在update_A
,您可以写print(date_register())
,但date_register
不会返回任何要打印的内容。
print
将字符串表示发送到控制台,不能用于分配。而是使用return
基本上强制函数调用解析为return
语句旁边的值。
例如:
def foo:
return "bar"
print(foo())
调用foo
时,它将解析为"bar"
,然后将其打印到控制台。有关print()
和return
的差异和用法的更多信息,请参阅here
要在date2
中使用update_A
,您应该将其返回并按如下方式分配:
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())
答案 1 :(得分:1)
我已经纠正了一两个其他小错误。
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)
这是输出。
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
显然,你需要弄清楚如何打印退房日期。