频道栏目
首页 > 资讯 > Python > 正文

python学习之InPut()和While循环知识讲解

18-07-27        来源:[db:作者]  
收藏   我要投稿

python学习之InPut()和While循环知识讲解

下面为学习笔记

message = input("Tell me something, and I will repeat it back to you: ")
print(message)
name = input("Please enter your name: ")
print("Hello, " + name + "!")
# 使用函数input()时, Python将用户输入解读为字符串。
# 为解决这个问题,可使用函数int(),它让Python将输入视为数值。函数int()将数字的字符
# 串表示转换为数值表示,如下所示:
age = input("How old are you ")
age = int(age)
if age >= 18:
 print('true')
# 处理数值信息时, 求模运算符( %)是一个很有用的工具,它将两个数相除并返回余数:
print(4%3)
# 如果你使用的是Python 2.7,请使用raw_input()而不是input()来获取输入。
current_number = 1
while current_number <= 5:
 print(current_number)
 current_number += 1
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)
# 要立即退出while循环,不再运行循环中余下的代码,也不管条件测试的结果如何,可使用
# 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语句,它
# # 不像break语句那样不再执行余下的代码并退出整个循环。
current_number = 0
while current_number < 10:
 current_number += 1
 if current_number % 2 == 0:
  continue
 print(current_number)
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 + ".")
相关TAG标签
上一篇:Prime Palindromes中的回文质数解决
下一篇:JDK Tomcat 环境变量的搭配
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站