《Python编程从入门到实践》第七章_用户输入和whlie循环

编码(python版)可查看这篇博客,理解一下编码python2与python3不同

参考博客《Python编程从入门到实践》第七章_用户输入和whlie循环,学习第七章内容

print("7.1 input()的工作原理")
message = input("Tell me something, and I will repeat it back to you: ")
print(message)
print("7.1.1编写清晰的程序")
name = input("please enter your name:")
print("Hello, " + name + "!")
prompt = "If you tell us who you are, we can personalize the message you see!"
prompt += "\nWhat is your first name?"
name = input(prompt)
print("\nHello, " + name + "!")
print("7.1.2 使用int()来获取数值输入")
age = input("How old are you?")
print(age)
age = int(age)
if age >= 18:
    print("True")
else:
    print("false")
####################################################################################
C:\Anaconda3\python.exe H:/python/venv/text
7.1.2 使用int()来获取数值输入
How old are you?30
30
True

Process finished with exit code 0
####################################################################################
#判断年龄是否达到做过山车的年龄
age = input("How old are you?")
if int(age) >=18:
    print("You can ride!")
else:
    print("You can't ride ")
####################################################################################
C:\Anaconda3\python.exe H:/python/venv/text
How old are you?20
You can ride!

Process finished with exit code 0
####################################################################################
print("7.1.3 求模运算符")
print(4 % 3)
print(5 % 3)
print(6 % 3)
print(7 % 2)
print("可以用来判断奇偶数")
number = input("Enter a number, and I'll tell you it's even or odd: ")
number = int(number)
if number % 2 == 0:
    print("The number " + str(number) + " is even.")
else:
    print("The number " + str(number) + " is odd.")
########################################################################################
C:\Anaconda3\python.exe H:/python/venv/text
7.1.3 求模运算符
1
2
0
1
可以用来判断奇偶数
Enter a number, and I'll tell you it's even or odd: 20
The number 20 is even.

Process finished with exit code 0
########################################################################################
# 在python2中使用raw_iput()来提示用户输入,
# 这个与python3中的input()是一样的,也将输入解读为字符串。
# python2中也存在input(),但它将用户输入解读为python代码,并尝试执行它。
print("7.2.1使用while循环")
current_number = 1
while current_number <= 5:
    print(current_number)
    current_number += 1
print("7.2.2让用户选择何时退出")
prompt = "Tell me something, and I will repeat it back you!"
prompt += "\nEnter 'quit' to end the program."
message = ""
while message != 'quit':
    message = input(prompt)
    if message != 'quit':
        print(message)
#####################################################################################
C:\Anaconda3\python.exe H:/python/venv/text
7.2.1使用while循环
1
2
3
4
5
7.2.2让用户选择何时退出
Tell me something, and I will repeat it back you!
Enter 'quit' to end the program.Hello
Hello
Tell me something, and I will repeat it back you!
Enter 'quit' to end the program.quit

Process finished with exit code 0
#######################################################################################
print("7.2.3使用标志")
prompt = "Tell me something, and I will repeat it back you!"
prompt += "\nEnter 'quit' to end the program."
active = True
while active:
    message = input(prompt)
    if message == 'quit':
        active = False
    else:
        print(message)
####################################################################################
C:\Anaconda3\python.exe H:/python/venv/text
7.2.3使用标志
Tell me something, and I will repeat it back you!
Enter 'quit' to end the program.Hello
Hello
Tell me something, and I will repeat it back you!
Enter 'quit' to end the program.quit

Process finished with exit code 0
###################################################################################
prompt = "\nPlease enter the name of a city you have visited: "
prompt += "\n(Enter 'quit' when you are finshed.)"
while True:
    city = input(prompt)
    if city == 'quit':
        break
    else:
        print("I'd love to go to " + city.title() + "!")
####################################################################################
C:\Anaconda3\python.exe H:/python/venv/text

Please enter the name of a city you have visited: 
(Enter 'quit' when you are finshed.)Hello
I'd love to go to Hello!

Please enter the name of a city you have visited: 
(Enter 'quit' when you are finshed.)ShangHai
I'd love to go to Shanghai!

Please enter the name of a city you have visited: 
(Enter 'quit' when you are finshed.)quit

Process finished with exit code 0
########################################################################################
print("7.2.5 在循环中使用continue")
current_number = 0
while current_number < 10:
    current_number += 1
    if current_number % 2 == 0:
        continue
    print(current_number)
print("7.2.6 避免无限循环")
x = 1
while x <= 5:
    print(x)
    x += 1
#########################################################################################
C:\Anaconda3\python.exe H:/python/venv/text
7.2.5 在循环中使用continue
1
3
5
7
9
7.2.6 避免无限循环
1
2
3
4
5

Process finished with exit code 0
#########################################################################################
print("7.3 使用while循环")
print("7.3.1在列表之间移动元素")
unconfirmed_users = ['alice', 'brain', 'candace']
confirmed_users = []

while unconfirmed_users:
    current_user = unconfirmed_users.pop()
    print("Verifying user: " + current_user.title())
    confirmed_users.append(current_user)
# 显示所有已验证的用户
print("The following users have been confirmed: ")
for confirmed_user in confirmed_users:
    print(confirmed_user.title())
print("7.3.2 删除包含特定值的所有列表元素")
pets = ['dog', 'cat', 'goldfish', 'cat']
while 'cat' in pets:
    pets.remove('cat')
print(pets)
print("7.3.3使用用户输入来填充字典")
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 or no) ")
    if repeat == 'no':
        polling_active = False
print("\n---Poll Results---")
for name, response in responses.items():
    print(name + " Would like to climb " + response + ".")
#####################################################################################
C:\Anaconda3\python.exe H:/python/venv/text
7.3 使用while循环
7.3.1在列表之间移动元素
Verifying user: Candace
Verifying user: Brain
Verifying user: Alice
The following users have been confirmed: 
Candace
Brain
Alice
7.3.2 删除包含特定值的所有列表元素
['dog', 'goldfish']
7.3.3使用用户输入来填充字典

What is your name? Mars
which mountain would you like to climb someday?aaa
Would you like to let another person respond?(yes or no) yes

What is your name? Micke
which mountain would you like to climb someday?DDD
Would you like to let another person respond?(yes or no) no

---Poll Results---
Mars Would like to climb aaa.
Micke Would like to climb DDD.

Process finished with exit code 0
######################################################################################

猜你喜欢

转载自blog.csdn.net/weixin_40807247/article/details/82078442