7.1
1. 变量所存储的字符串过长,需要换行续写的解决方法:
使用 += 并配合换行符达到预期效果
或者换行符放在第一行后面\n
2. 数值转字符 ,用 str( ) 函数
3. 字符转数值,用 int( ) 函数
4. 求模运算(即求余数),用 %
7.1
continue :跳出当前循环
break : 跳出所有循环
标志:添加一个变量,放在循环之前,作为程序是否活动的标志,并在任何事件导致标志的值被改变为False 时让程序停止运行。
例如如下程序:
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "
active = True
message = ""
while active:
message = input(prompt)
if message == "quit":
active = False
else:
print(message)
点菜程序,例子如下:
prompt = "\nAdd your food:"
all_food = ""
foods = ""
while foods != 'quit':
foods = input(prompt)
if foods == "":
print('Please enter your foods! It is null now')
elif foods == "quit":
break
else:
print('Your food:' + foods + " are ready")
all_food = all_food + foods +" , "
print("You had added :"+all_food)
print("You had added :"+all_food)