发布于2019-08-08 11:08 阅读(496) 评论(0) 点赞(2) 收藏(3)
input()函数:让程序运行暂停,等待用户输入。
message = input('Tell me something, and I will repeat it back to you: ') print(message)
运行结果:
Tell me something, and I will repeat it back to you: Hello Python!
Hello Python!
name = input("Please enter your name: ") print("Hello, " + name + "!") Please enter your name: hery Hello, hery!
提示信息超过一行时:
prompt = "If you tell us who you are, we can personalize the messages you see." prompt += '\nWhat is your name? ' name = input(prompt) print("\nHello, " + name + "!")
age = input("How old are you? ") print(type(age)) How old are you? 12 <class 'str'>
通过input()函数输入的信息以字符串的形式存储,若需要将输入作为数值使用怎么办呢?
可以使用int()函数将其转换为数值表示:
height = input("How tall are you, in inches? ") height = int(height) if height >= 36: print("\nYou're tall enough to ride!") else: print("\nYou'll be able to ride when you're a little older.")
求模运算符(%):求得两数相除返回的余数。
可用于判断一个数是奇数还是偶数:
number = input("Enter a number, and I'll tell you if it is even or odd: ") number = int(number) if number % 2 == 0: print('\nThe number ' + str(number) + ' is even.') else: print('\nThe number ' + str(number) + ' is odd.')
运算符两端的元素类型要一致,故print语句中又需要将数值型通过str()函数转换为字符型。
for循环是针对集合中的每个元素的一个代码块,而while循环是不断的运行,直到指定条件不满足。
current_number = 1 while current_number <= 5: print(current_number) current_number += 1
运行结果:
1 2 3 4 5
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) print(message)
运行结果:
Tell me something , and I will repeat it back to you: Enter 'quit' to end the program. Hello Python Hello Python Tell me something , and I will repeat it back to you: Enter 'quit' to end the program. Hello 0629 Hello 0629 Tell me something , and I will repeat it back to you: Enter 'quit' to end the program. quit quit
输入为 quit 时循环结束。
若不想将 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)
在要求很多条件都满足的情况下才继续运行的程序中,可定义一个变量,用于判断整个程序是否处于活动状态,这个变量称为标志。
prompt = "\nTell me something , and I will repeat it back to you:" prompt += "\nEnter 'quit' to end the program. " active = True while active: message = input(prompt) if message == 'quit': active = False else: print(message)
敲代码的时候把 active = False 敲成了 active = 'False',然后输入quit还一直执行循环,哈哈哈
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() + "!")
Note: Python循环(while循环、for循环)中都可使用break语句来推出循环。
循环中使用continue,会返回大循环开头,并根据条件测试结果决定是否继续执行循环:
current_number = 0 while current_number < 10: current_number += 1 if current_number % 2 == 0: continue else: print(current_number)
运行结果:
1 3 5 7 9
x = 1 while x < 5: print(x) x += 1
上述的代码块中,若漏写了代码行 x += 1,这个程序将没完没了地运行。可按Ctrl + C,也可关闭显示程序输出的终端窗口,或关闭编辑器,结束无限循环。
作者:奋斗
链接:https://www.pythonheidong.com/blog/article/13524/08737044949cf1b4c54b/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!