码迷,mamicode.com
首页 > 编程语言 > 详细

python学习6

时间:2017-12-14 12:03:34      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:简介   选择   16px   eve   while   lease   nal   please   问号   

一、函数input()的工作原理:

  有时候,提示可能超过一行,例如,你可能需要指出获取特定输入的原因。在这种情况下,
可将提示存储在一个变量中,再将该变量传递给函数input()。这样,即便提示超过一行,input()
语句也非常清晰。

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 + "!")

  这个示例演示了一种创建多行字符串的方式。第1行将消息的前半部分存储在变量prompt中;
在第2行中,运算符+=在存储在prompt中的字符串末尾附加一个字符串
最终的提示横跨两行,并在问号后面包含一个空格,这也是出于清晰考虑:

If you tell us who you are, we can personalize the messages you see.
What is your first name? Eric
Hello, Eric!

二、使用int()来获取数值输入:

  为解决这个问题,可使用函数int(),它让Python将输入视为数值。函数int()将数字的字符
串表示转换为数值表示,如下所示:

>>> age = input("How old are you? ")
How old are you? 21
? >>> age = int(age)
>>> age >= 18
True

  如何在实际程序中使用函数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.")



How tall are you, in inches? 71
Youre tall enough to ride!

注意:

如果你使用的是Python 2.7,应使用函数raw_input()来提示用户输入。这个函数与Python 3
中的input()一样,也将输入解读为字符串。

三、while 循环简介:

  for循环用于针对集合中的每个元素都一个代码块,而while循环不断地运行,直到指定的条
件不满足为止。

current_number = 1
while current_number <= 5:
print(current_number)
current_number += 1

四、让用户选择何时退出

  可使用while循环让程序在用户愿意时不断地运行,如下面的程序parrot.py所示。我们在其中
定义了一个退出值,只要用户输入的不是这个值,程序就接着运行:

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)


Tell me something, and I will repeat it back to you:
Enter quit to end the program. Hello everyone!
Hello everyone!
Tell me something, and I will repeat it back to you:
Enter quit to end the program. Hello again.
Hello again.
Tell me something, and I will repeat it back to you:
Enter quit to end the program. quit

五、使用标志

  下面来在前一节的程序parrot.py中添加一个标志。我们把这个标志命名为active(可给它指
定任何名称),它将用于判断程序是否应继续运行:

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)

  在while循环中,我们在用户输入后使用一条if语句来检查变量message的值。如果用户输入
的是‘quit‘(见?),我们就将变量active设置为False,这将导致while循环不再继续执行。如果
用户输入的不是‘quit‘(见?),我们就将输入作为一条消息打印出来。

六、使用break 退出循环

  要立即退出while循环,不再运行循环中余下的代码,也不管条件测试的结果如何,可使用
break语句。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() + "!")



Please enter the name of a city you have visited:
(Enter ‘quit‘ when you are finished.) New York
I‘d love to go to New York!
Please enter the name of a city you have visited:
(Enter ‘quit‘ when you are finished.) San Francisco
I‘d love to go to San Francisco!
Please enter the name of a city you have visited:
(Enter ‘quit‘ when you are finished.) quit

 

九、在循环中使用continue

  要返回到循环开头,并根据条件测试结果决定是否继续执行循环,可使用continue语句,它
不像break语句那样不再执行余下的代码并退出整个循环。例如,来看一个从1数到10,但只打印
其中偶数的循环:

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

1
3
5
7
9    

十、避免无限循环;

  每个while循环都必须有停止运行的途径,这样才不会没完没了地执行下去。例如,下面的
循环从1数到5:

x = 1
while x <= 5:
print(x)
x += 1

但如果你像下面这样不小心遗漏了代码行x += 1,这个循环将没完没了地运行:

# 这个循环将没完没了地运行!
x = 1
while x <= 5:
print(x)

 

python学习6

标签:简介   选择   16px   eve   while   lease   nal   please   问号   

原文地址:http://www.cnblogs.com/a255/p/8036607.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!