第7章:用户输入和while循环

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值