PYTHON--从入门到精通第七章代码练习

本文介绍了Python编程的基础知识,包括如何编写清晰的程序、处理用户输入、使用循环和条件语句进行流程控制,以及如何通过列表操作和字典管理数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

7.1.1 编写清晰的程序

prompt = "if you tell us who are you "
prompt += "\nwhat is you first name "

name = input(prompt)
print("\nhello," + name + “!”)

7.1.2使用int()来获取数值输入

int() 将默认输出的字符串转换成数值

age = input(‘how old are you ?’)
print(int(age) >= 18)

判断一个人人是否满足过山车的身高要求

height = input(“how tall are you , in inches ?”)
height = int(height)

if height >= 36:
print("\nyou are tall enough to ride")
else:
print("\nyou’ll be able to ride when you are a little older")

求模运算符,奇偶数判断

number = input(“enter a number , and i will tell you if it is even or old :”)
number = int(number)

if number % 2 == 0:
print("\nthe number " + str(number) + "is even “)
else:
print(”\n the number " + str(number) + "is old ")

7.2.1使用while循环,循环1-5

current_number = 1
while current_number <= 5:
print(current_number)
current_number += 1

7.2.2让用户选择何时退出

prompt = “tell me something , and i will repeat it back to you :\n”
prompt += "enter ‘quit’ to end the progranm : "

message = “”
while message != ‘quit’:
message = input(prompt)
if message != ‘quit’:
print(message)

7.2.3使用标志(程序的交通信号灯)

prompt = “tell me something , and i will repeat it back to you :\n”
prompt += "enter ‘quit’ to end the progranm : "

active = True
while active:
message = input(prompt)
if message == ‘quit’:
active = False

7.2.4 S使用break 退出循环,让用户指出用户到过的地方

prompt = “\n please enter the name of a city you have visited :”
prompt += "\n enter 'quit when you finished "

while True:
city = input(prompt)

if city == 'quit':
    break
else:
    print("i would like to " + city.title() + "!")

7.2.5 在循环中使用continue打印出奇数

current_number = 0
while current_number < 10:
current_number += 1
if current_number % 2 == 0:
continue
print(current_number)

7.3.1 在列表之间移动元素

首先,创建一个待验证用户列表

和一个用于存储已验证用户的空列表

unconfirmed_users = [‘qq’, ‘cc’, ‘ss’]
confirm_users = []

验证每个用户,直到没有验证的用户为止

将每个经过验证的列表都移到已验证的用户中

while unconfirmed_users:
current_user = unconfirmed_users.pop() # pop()用于移除列表中的元素并且返回该元素的值
print(“verifying user” + current_user.title())
confirm_users.append(current_user)

显示所有已经验证用户

print("\n the following user is confirmed:")
for confirm_user in confirm_users:
print(confirm_user.title())

7.3.2 删除包含特定值的所有列表元素

pets = [‘11’, ‘122’, ‘aad’, ‘11’, ‘234’]
print(pets)

while ‘11’ in pets:
pets.remove(‘11’)
print(pets)

使用用户输入来填充字典

responses = {}

设置一个标志,指出调查是否继续

poling_active = True
while poling_active:
# 提示输入者的名字和回答
name = input("\n what is you name ? ")
response = input(“which moutain would you like to climb some day ?”)

# 将名字和回答存储
responses[name] = response

# 看是否还有其他人参与回答
repeat = input("would you like to let anather person response ? (yes or no ):")
if repeat == 'no':
    poling_active = False

调查结束,显示结果

print(“poll results”)
for name, response in responses.items():
print(name + " like to climb " + response)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值