1. 程式人生 > >name 'cap_name' is not defined

name 'cap_name' is not defined

書上程式碼塊

"""
Spyder Editor
This is a temporary script file.
"""
#hello.py
def say_hello_to(name):
    cap_name=name.capitalize()
print('hello'+cap_name+',how are you ?')

兩個問題

1. print()函式應該在函式塊內
2. 呼叫say_hello_to(name)函式,輸入name時,應該加上引號‘name’或者”name”

正確程式碼

"""
Spyder Editor
This is a temporary script file.
"""
#hello.py def say_hello_to(name): cap_name=name.capitalize() print('hello'+' '+cap_name+',how are you ?')

呼叫時

say_hello_to('liming')
say_hello_to("liming")

結果

hello Liming,how are you ?
hello Liming,how are you ?