Python 基础:函数和 While 循环:

B站影视 2024-12-02 06:35 1

摘要:def function_name(parameters): # Function body function_namedef my_function(name= input("What is your name? - ")): print(f"Hello,

内置函数

Python 中的函数是执行特定任务的可重用代码块。它们允许您将代码组织到可管理的部分并促进代码重用。

def function_name(parameters): # Function body function_namedef my_function(name= input("What is your name? - ")): print(f"Hello, {name}!") my_function # Output: Hello, Guest!my_function("Welcome !") # Output: Hello, Welcome!>>What is your name? - NidhiHello, Nidhi!Hello, Welcome !!

可以在函数中调用 Function:

def my_function(name= input("What is your name? - ")): print(f"Hello, {name}!")def fun: print(f"This is for calling def my_function.") my_functionfun>>What is your name? - NidhiThis is for calling def my_function.Hello, Nidhi!

已经了解了 “for in” 循环,让我们使用它:

def my_function(name= input("What is your name? - ")): print(f"Hello, {name}!")def fun: print(f"This is for calling def my_function.") my_functionfor say in range(6): fun>>What is your name? - NidhiThis is for calling def my_function.Hello, Nidhi!This is for calling def my_function.Hello, Nidhi!This is for calling def my_function.Hello, Nidhi!This is for calling def my_function.Hello, Nidhi!correct_password = "secret"user_input = ""while not user_input == correct_password: # We can use while not / != user_input = input("Enter the password: ")print("Access granted!")correct_password = "secret"user_input = ""while user_input != correct_password: # We can use while not / != user_input = input("Enter the password: ")print("Access granted!")correct_password = "secret"user_input = ""while user_input != correct_password: user_input = input("Enter the password: ") if user_input == correct_password: print("Access granted!") else: print("Incorrect password, please try again.")>>Enter the password: 123Incorrect password, please try again.Enter the password: 258Incorrect password, please try again.Enter the password: addaIncorrect password, please try again.Enter the password: secretAccess granted!

来源:自由坦荡的湖泊AI一点号

相关推荐