Python 用户输入input()和While循环的用法——7

#!/usr/bin/env python
#-- coding:utf-8 --

#1.用户输入input()用法,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 + "!")

#2.分行 方法1

prompt = "If you tell us who you are, we can personalize the messages you see. \nWhat is your first name?"

#2.分行 方法2

prompt = "If you tell us who you are, we can personalize the messages you see."
prompt += "\nWhat is your first name? "
name = input(prompt)
print("\nHello, " + name + "!")

#3.使用int()将str转化为整型数字

age = input("How old are you? ")
age = int(age)
if age > 18:
    print("T")
else:
    print("F")

#4.求膜 %,即求余数,可以用来判断奇偶数

num = 7 % 2
print(num)

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

#6.用户输入quit才会停止循环

prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "

message = ""
while message != 'quit':
    message = input(prompt)
    if message != 'quit':
        print(message)

#7.添加标志,False跳出循环,在6的基础上

prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "

message = ""
active = True
while active:
    message = input(prompt)
    if message == 'quit':
        active = False
    else:
        print(message)

#8.break跳出循环

prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "

message = ""
active = True
while active:
    message = input(prompt)
    if message == 'quit':
        break
    else:
        print(message)

#9.continue用法:遇到continue就不执行后面的代码了,直接跳到循环的开始再执行

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

#10.在列表之间移动元素

unconfirmed_users = ['alice', 'brian', 'candace']
confirmed_users = []

while unconfirmed_users:
    current_user = unconfirmed_users.pop()
    print(current_user)
    confirmed_users.append(current_user)    # append把元素加到列表最后
#print(confirmed_users)

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

#11.删除列表中所有特定值

pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
print(pets)
while 'cat' in pets:
    pets.remove('cat')  # 删除所有的'cat'
print(pets)

#12.使用用户输入来填充字典

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 respond? (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 + ".")
  • 1
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

打赏
文章很值,打赏犒劳作者一下
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页

打赏

wolves_liu

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

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

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

打赏作者