• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

关于python:Kangrui‘s-Python-Learning-Dairy-20200721

python 搞代码 3年前 (2022-02-20) 26次浏览 已收录 0个评论

内容对于用户输出,while循环以及函数
明天所学内容局部须要输出,所以不附运行后果,欢送大家本人尝试


以下内容为用户输出和while循环

# input()工作原理,实质是输出字符串
message = input("Tell me something, and I will repeat it back to you: ")
print(message)

name = input("Please enter your name: ")
print("Hello, " + name + "!")

# int()工作原理,实质是将输出视为数值
height = input("How tall are you, in inches? ")
height = int(height)
if height >= 36:
    print("\nYou're tall enough to ride! ")
else:
    print("\nYou'll be able to ride when you're a little older.")

# 求模运算符%,省略

# while循环
current_number = 1
while current_number <= 5:
    print(current_number)
    current_number += 1

# 应用break退出循环
prompt = "\nPlease enter the name of a city you have visited: "
prompt += "\n(Enter 'quit' when you are finished.)"
while True:
    city = input(prompt)
    if city == 'quit':
        break
    else:
        print("I'd love to go to " + city.title() + "!")

# 在循环中应用continue,满足条件时不再执行接下里的程序,进入下一循环
current_number = 0
while current_number < 10:
    current_number += 1
    if current_number % 2 == 0:
        continue
    print(current_number)

以下内容为函数

# 向函数传递信息
def greet_user(username):
    # 形参username
    print("Hello, " + username.title() + "!")

greet_user('jesse')  # 实参'jesse'

# 传递实参
def describe_pet(animal_type, pet_name):
    print("\nI have a " + animal_type + ".")
    print("My " + animal_type + "'s name is " + pet_name.title() + ".")


describe_pet('hamster', 'harry')  #  地位实参,最根本的是基于实参程序,不能随便调换
describe_pet(animal_type = 'hamster', pet_name = 'harry')  # 关键字实参,能够换地位


def describe_pet(pet_name, animal_type = 'dog'):  # 蕴含默认值,且默认值须要在前面
    print("\nI have a " + animal_type + ".")
    print("My " + animal_type + "'s name is " + pet_name.title() + ".")

describe_pet(pet_name = 'willie')

# 返回值
def get_formatted_name(first_name, last_name):
    full_name = first_name + ' ' + last_name
    return full_name.title()

musician = get_formatted_name('jimi', 'hendrix')
print(musician)

# 使参数可选
def get_formatted_name(first_name, middle_name, last_name):
    if middle_name:
        full_name = first_name + ' ' + middle_name + ' ' + last_name
    else:
        full_name = first_name + ' ' + last_name
    return full_name.title()

musician = get_formatted_name('jimi','lee','hendrix')
print(musician)

# 返回字典
def build_person(first_name, last_name):
    person = {'first':first_name, 'last':last_name}
    return person

person = build_person('jimi', 'hendrix')
print(person)


# 传递列表
def greet_users(names):
    for name in names:
        msg = "Hello, " + name.title() + "!"
        print(msg)

usernames = ['hannah', 'ty', 'margot']
greet_users(usernames)

# 禁止函数批改列表
function_name(list_name[:])  #用切片表示法[:]创立列表的正本

# 传递任意数量的实参
    # 应用形参名*names,创立一个空元组,放入所有收到的值
    # 能够和一般实参联合应用

# 将函数存储在模块中,应用import module_name
# 调用模块中的某个函数,module_name.function_name
# 只导入特定的函数,from module_name import function_1,function 2
# 应用as指定别名,from module import function as anothername;import module as anothername
# 导入模块中所有的函数,from module import *

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:关于python:Kangrui‘s-Python-Learning-Dairy-20200721
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址