2.21

  1 # greeter 7.11
  2 
  3 # promt = "If you tell us who you are, we can personalize messages you see."
  4 # promt += "\nWhat is your first name?"
  5 #
  6 # name = input(promt)
  7 # print ("\nHello," + name + "!")
  8 """重点 这个示例演示了一种创建多行字符串的方式。第1行将消息的前半部分存储在变量prompt 中;在第2行中,运算符+= 在存储在prompt 中的字符串末尾附加一个字符串。
  9 最终的提示横跨两行,并在问号后面包含一个空格,这也是出于清晰考虑:"""
 10 
 11 # # age = input("How old are you?")
 12 # #
 13 # # age >= 18
 14 # height = input("How tall are you, in inches?")
 15 # height = int(height)
 16 #
 17 # if height >= 36:
 18 #     print("\nYou\'re tall enough to ride!")
 19 # else:
 20 #     print("\nYou\'ll be able to ride when you\'er a litter older.")
 21 #
 22 # number = input("Enter a number and I\'ll tell you if it\'s even or odd:")
 23 # number = int(number)
 24 #
 25 # if number % 2 == 0:
 26 #     print("\nThe number " + str(number) + " is even.")
 27 # else:
 28 #     print("\nThe number " + str(number) + " is odd.")
 29 #
 30 # #rennsyu7 -14
 31 #
 32 # rent_car = input("What car do you want to rent?")
 33 # print("Let me see if I can find you a " + rent_car)
 34 #
 35 # ninnzu = input("何名様でございますか?")
 36 # ninnzu = int(ninnzu)
 37 #
 38 # if ninnzu > 8:
 39 #     print("大変申し訳ございませんが、うちは8名様の席は用意しておりません。")
 40 # else:
 41 #     print(str(ninnzu) + "名様ご来店です。")
 42 #
 43 # number = input("Please tell me one number,and I\'ll tell you the number is a multiple of 10 or not.")
 44 # number = int(number)
 45 #
 46 #
 47 # if number % 10 == 0:
 48 #     print(str(number) + "is a multiple of 10.")
 49 # else:
 50 #     print(str(number) + " isn\'t a multiple of 10.")
 51 
 52 # current_number = 1
 53 # while current_number <= 5:
 54 #     print(current_number)
 55 #     current_number += 1
 56 
 57 """
 58 message = input("What car do you want to rent?")
 59 while message != 'exit':
 60     rent_car = input("What car do you want to rent?")
 61     print("Let me see if I can find you a " + rent_car)#问题 此行为何不显示 下文解析 与文中例思维不同
 62 
 63 prompt = "\nTell me something, and I will repeat it back to you:"
 64 prompt += "\nEnter 'quit' to end the program."
 65 message = "" #此处用变量message来储存用户输入的值
 66 while message != 'quit':
 67     message = input(prompt)
 68     print(message)
 69 
 70 在❶处,我们定义了一条提示消息,告诉用户他有两个选择:要么输入一条消息,要么输入退出值(这里为'quit' )。接下来,我们创建了一个变量——message (见❷),
 71 用于存储用户输入的值。我们将变量message 的初始值设置为空字符串"" ,让Python首次执行while 代码行时有可供检查的东西。Python首次执行while 语句时,需要
 72 将message 的值与'quit' 进行比较,但此时用户还没有输入。如果没有可供比较的东西,Python将无法继续运行程序。为解决这个问题,我们必须给变量message 指定一个
 73 初始值。虽然这个初始值只是一个空字符串,但符合要求,让Python能够执行while 循环所需的比较。只要message 的值不是'quit' ,这个循环(见❸)就会不断运行。
 74 首次遇到这个循环时,message 是一个空字符串,因此Python进入这个循环。执行到代码行message = input(prompt) 时,Python显示提示消息,并等待用户输入。不管用
 75 户输入是什么,都将存储到变量message 中并打印出来;接下来,Python重新检查while 语句中的条件。只要用户输入的不是单词'quit' ,Python就会再次显示提示消息并等
 76 待用户输入。等到用户终于输入'quit' 后,Python停止执行while 循环,而整个程序也到此结束:
 77 
 78 这个程序很好,唯一美中不足的是,它将单词'quit' 也作为一条消息打印了出来。为修复这种问题,只需使用一个简单的if 测试:
 79 """
 80 
 81 # #看例子后重新修改原代码
 82 # message = "What car do you want to rent?"
 83 # while message != 'exit':
 84 #     message = input(message)#进入循环后message的值就变为用户输入的值
 85 #     print("Let me see if I can find you a " + message)
 86 
 87 # message = input("What car do you want to rent?")
 88 # while message != 'exit':
 89 #     rent_car = input("What car do you want to rent?")
 90 #     print("Let me see if I can find you a " + rent_car)#问题 此行为何不显示 下文解析 与文中例思维不同
 91 #
 92 # prompt = "\nTell me something, and I will repeat it back to you:"
 93 # prompt += "\nEnter 'quit' to end the program."
 94 # message = "" #此处用变量message来储存用户输入的值
 95 # while message != 'quit':
 96 #     message = input(prompt)
 97 #     if message != 'quit':
 98 #         print(message)
 99 #
100 #
101 # """
102 # 7.2.3 使用标志 类似于一个开关
103 # 在前一个示例中,我们让程序在满足指定条件时就执行特定的任务。但在更复杂的程序中,很多不同的事件都会导致程序停止运行;在这种情况下,该怎么办呢?
104 # 例如,在游戏中,多种事件都可能导致游戏结束,如玩家一艘飞船都没有了或要保护的城市都被摧毁了。导致程序结束的事件有很多时,如果在一条while 语句中检查所有这些
105 # 条件,将既复杂又困难。
106 # 在要求很多条件都满足才继续运行的程序中,可定义一个变量,用于判断整个程序是否处于活动状态。这个变量被称为标志 ,充当了程序的交通信号灯。你可让程序在标志
107 # 为True 时继续运行,并在任何事件导致标志的值为False 时让程序停止运行。这样,在while 语句中就只需检查一个条件——标志的当前值是否为True ,并将所有测试(是
108 # 否发生了应将标志设置为False 的事件)都放在其他地方,从而让程序变得更为整洁。
109 # 下面来在前一节的程序parrot.py中添加一个标志。我们把这个标志命名为active (可给它指定任何名称),它将用于判断程序是否应继续运行:
110 # """
111 #
112 # prompt ="\nTell me something,and I will repeat it back to you."
113 # prompt += "\nEnter 'quit' to end the program."
114 #
115 # active = True
116 # while active:
117 #     message = input(prompt)
118 #
119 #     if message == 'quit':
120 #         active = False
121 #     else:
122 #         print(message)
123 
124 # #练习7-4  p66
125 # prompt = "\nWhat kind of topping do you want?"
126 # prompt +="\nPlease input what you want.Enter'quit' to end."
127 # message = ""
128 # topping =[]
129 # oder = True
130 # while oder:
131 #     #input(prompt) 此行为多余
132 #     if message == 'quit':
133 #         oder = False
134 #     else:
135 #         message = input(prompt)
136 #         print("You odered " + message + ".")
137 #         #topping.append(str(input()))   # 问题为何此处需要按回车? 添加str后无变化。 #重点 已解决。 须多用变量来存放通过函数所得的值
138 #         topping.append(message)#将input()函数转换为储存用户输入字符串的变量名后,运行至此处无须再次等待输入。
139 #         print(topping)
140 
141 prompt = "\nPlease tell me your age."
142 prompt += "\nEnter 'quit' to end."
143 message = ''
144 switch = True
145 while switch:
146     input(prompt)
147     message = input(message)
148     if message == 'quit':
149         switch = False
150 
151     #message = int(message)
152     if message < 3:
153         price = 0
154     if message >= 3 and message <= 12:
155         price = 10
156     if message > 12:
157         price = 15
158     print(price)
View Code

猜你喜欢

转载自www.cnblogs.com/phyllissRyo/p/10414393.html
今日推荐