我是python中的绝对初学者,我试图理解为什么最后一行不起作用。 PyCharm声明" n"是一个未解决的引用,我已确保该函数返回字符串输入。线本身或函数是一个问题吗?
def askforname():
n = str(input("Please enter your name: "))
return n
def printchoice():
print("You are starving and go down to the kitchen.")
print("You are now confronted with an eon defining choice, spaghetti or lasagna")
def hello(n):
print("Hello " + n + "!")
def choiceone():
choicelasagna = str(input("Type \"Lasagna\" to pick lasagna and type \"Spaghetti\" to pick spaghetti: "))
return choicelasagna
def kalle():
ch = choiceone()
if ch == "Lasagna" or "lasagna":
print("You cooked some warm lasagna and enjoyed it")
else:
print("You cooked some spaghetti and enjoyed it")
def main():
n = askforname()
hello(n)
printchoice()
kalle()
def goodbye(n):
print("Goodbye" + n)
def request():
print("")
main()
goodbye(n)
答案 0 :(得分:1)
n
仅在函数main
中定义。要在n
之外访问main
,您需要从main
def main():
n = askforname()
hello(n)
printchoice()
kalle()
return n
n = main()
并捕获返回的值:
n
然后,您可以根据需要访问print(n)
:
Private Sub Command95_Click()
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Sheet1", CurrentProject.Path & "\PR1.xls", False, A3
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Performance_report_HW_H1story", CurrentProject.Path & "\Performance report HW.xls", False, A3
'Change format in Excel cells
'Dim exApp As Excel.Application
' Dim exBook As Excel.Workbook
' Dim exSheet As Excel.Worksheet
' Dim BookName As String
'Set exApp = New Excel.Application
'BookName = CurrentProject.Path & "\Performance report HW.xls"
'Set exBook = exApp.Workbooks.Open(BookName)
'Set exSheet = exBook.Worksheets(1)
'exSheet.Activate
'exSheet.Range("A3", "AQ30000").Font.Size = 8
'exSheet.Range("A3", "AQ30000").Font.Name = "Arial"
'exSheet.Range("l3", "l30000").NumberFormat = "hh:mm"
'exSheet.Range("o3", "o30000").NumberFormat = "hh:mm"
'Set exSheet1 = exBook.Worksheets(2)
'exSheet.Activate
'exSheet1.Range("A3", "AQ30000").Font.Size = 8
'exSheet.Range("A3", "AQ30000").Font.Name = "Arial"
'exSheet.Range("l3", "l30000").NumberFormat = "hh:mm"
'exSheet.Range("o3", "o30000").NumberFormat = "hh:mm"
'exBook.Save
'exApp.Interactive = True
' exBook.Close
' Set exSheet = Nothing
' Set exBook = Nothing
' Set exApp = Nothing
MsgBox "File stored in " & CurrentProject.Path & "\Performance report HW.xls", vbDefaultButton1
DoCmd.SetWarnings True
End Sub
答案 1 :(得分:0)
您的代码存在的问题是,您要将变量n
传递给goodbye(n)
函数而不先定义n
。由于名称n
未指向任何值,因此它是未解决的引用。
在n
中定义main()
并不会使main()
以外的功能可以访问它。这样的规则称为范围规则。变量的范围是可以自由访问的代码区域,包括定义它的区域。
在这种情况下,n
的范围仅限于main()
功能。这意味着n
以外的功能无法使用main()
;因此n
未定义,您无法将其(不存在)值传递给另一个函数。
您可以在 n
之外定义main()
,以使其可供其他功能使用:
...
def main():
hello(n) # here n references a global variable
printchoice()
kalle()
...
# n is defined globally outside main()
n = askforname()
main()
goodbye(n)
答案 2 :(得分:0)
问题是那个再见(n)不知道什么' n'是,As' n'是main()函数的局部变量,其范围仅限于Main。其他函数无法识别此变量,因为其范围不是全局的。它是main()的局部变量。 要访问' n'在其他函数中,您可以在调用main()函数之前在全局声明它,与其他函数分离。
#To Change Global Variable In Funtions Add Global Before The Variable
def askforname():
global n = str(input("Please enter your name: "))
return n