Python 3 《while》入门练习

#!/usr/bin/env python
# -*- coding:utf-8 -*- 
# author: Christal date: 2021/11/20

# -------input应用--------------
message = input("Tell me something, and I will repeat it back to you: ")
print(message)    # input将输入的内容解读为字符串

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

prompt = "If you tell us who you are, we can personalize the messages you see."
prompt += "\nWhat is your first name? "  # 运算符+=在存储在prompt中的字符串末尾附加一个字符串
name = input(prompt)
print("\nHello, " + name + "!")

#-----int()获取数值输入---------
number =input("Enter a number, and I'll tell you if it's even or odd: ")
#number >=18        #运行这行代码会报错:
                    # TypeError: '>=' not supported between instances of 'str' and 'int'
number = int(number)
if number%2 ==0:        # 取余运算判断奇偶性
    print("\nThe number " + str(number) + " is even.")
else:
    print('\nThe number ' + str(number) + " is odd.")


#-----------While语句---------------
    #---for循环用于针对集合中的每个元素都一个代码块,
        # -----而while循环不断地运行,直到指定的条件不满足为止。
current_number =1
while(current_number <= 5):  #知道()中调减不满足,则不再执行下面的print语句
    print(current_number)
    current_number += 1
print("Finally, the number is " + str(current_number))


prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "
message = ""   # 定义初始值,保证while在第一次循环(还没有输入值)时,能够有比较的内容
while message != 'quit':
    message = input(prompt)
    if message != 'quit':   # 保证用户输入‘quit’之后不再打印出quit
        print(message)

# 上述一段方程的改进版本,使函数看起来更加干净简洁
prompt = "\n Tell me something, and I will repeat it back to you:"
prompt += "\n Enter 'quit' to end the program. "
active = True  #active为flag,类似于交通灯
while active:
    message = input(prompt)
    if message == 'quit':
        active = False
    else:
        print(message)


#--------break语句的使用------------
        # 要立即退出while循环,不再运行循环中余下的代码,也不管条件测试的结果如何,可以用 break
prompt = "\nPlease enter the name of a city you have visited:"
prompt += "\n(Enter 'quit' when you are finished.) "
while True:                  #程序将一直运行,知道遇到break
    city = input(prompt)

    if city == 'quit':
        break
    else:
        print("I'd love to go to " + city.title() + "!")


#-------- continue语句的使用------------
              # 要返回while 循环开头,并根据测试结果决定是否继续执行循环,可使用continue 语句
current_number = 0
while current_number < 10:
    current_number += 1
    if current_number % 2 == 0:
        continue              # 如果程序执行到continue,则返回
    print(current_number)  # 运行结果是 1 3 5 7 9


#--------在列表之间移动元素------------
# 首先,创建一个待验证用户列表
# 和一个用于存储已验证用户的空列表
unconfirmed_users = ['alice', 'brian', 'candace']
confirmed_users = []
# 验证每个用户,直到没有未验证用户为止, 将每个经过验证的列表都移到已验证用户列表中
while unconfirmed_users:
    current_user = unconfirmed_users.pop()
    print("Verifying users are:  " + current_user.title())
    confirmed_users.append(current_user)

    # 显示所有已验证的用户
print("\nThe following users have been confirmed:")
for i in confirmed_users:
    print(i.title())


#---------删除含有特定值的列表元素-----------
pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
print(pets)
while 'cat' in pets:
    pets.remove('cat')
print(pets)


#-----******--------用户输入来填充字典-----******-----
responses = {}

#设置一个标志,指出调查是否继续
polling_active = True

while polling_active:
    # 提示输入被调查者的名字和回答
    name = input("\nWhat is your name? ")
    response = input("Which mountain would you like to climb someday? ")

    #将答卷写入字典
    responses[name] = response

    #看是否海与人要参与调查
    repeat = input("Would you like to let another person response? (yes/no)")
    if repeat == 'no':
        polling_active = False

    #调查结束,显示结果
print("\n----Poll Results----")
for name, response in responses.items():
    print(name + " Would like to climb " + response + ".")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清韵逐梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值