python-9-emmm~while语句可能是与用户交互的绝佳工具!

学习内容:《python编程:从入门到实践》第二版

知识点:

while循环、break停止运行、continue从头开始循环

练习内容:

练习7-4:比萨配料 编写一个循环,提示用户输入一系列比萨配料,并在用户输入'quit'时结束循环。每当用户输入一种配料后,都打印一条消息,指出我们会在比萨中添加这种配料。

练习7-5:电影票 有家电影院根据观众的年龄收取不同的票价:不到3岁的观众免费;3~12岁的观众收费10美元;超过12岁的观众收费15美元。请编写一个循环,在其中询问用户的年龄,并指出其票价。

练习7-6:三种出路 以不同的方式完成练习7-4或练习7-5,在程序中采取如下做法。在while循环中使用条件测试来结束循环。使用变量active来控制循环结束的时机。使用break语句在用户输入'quit'时退出循环。(答案都在7-45里了)

练习7-7:无限循环 编写一个没完没了的循环,并运行它(要结束该循环,可按Ctrl + C,也可关闭显示输出的窗口)。(答案在7-4第一个代码里)

我的代码&运行结果:

练习7-4

比萨配料 编写一个循环,提示用户输入一系列比萨配料,并在用户输入'quit'时结束循环。每当用户输入一种配料后,都打印一条消息,指出我们会在比萨中添加这种配料。

prompt = "\n Enter the ingredients of pizza"
prompt += '(Enter "quit" when there is no other ingredients)'
ingredients = input(prompt)
while ingredients != 'quit':
    if ingredients != 'quit':
        print(f"add {ingredients} in pizza")
        continue
    else:
        break

 运行结果:

输入banana

 输入quit

 continue 在这里没什么用,只是在循环我打印的文字,我以为是会重新跳回输入input,让我重新输入,实际跳回已经输入的内容中。

因为刚开始 input没有东西,所以要交互,但后续会形成无限循环,实际上没有终止。

再回去看书,这个时候第一次意识到,为什么这章后续要讲3个如何退出程序的方法,这里看是会自动循环。

prompt = "\n Enter the ingredients of pizza"
prompt += '(Enter "quit" when there is no other ingredients)'
ingredients = ""
while ingredients != 'quit':
    ingredients = input(prompt)
    print(ingredients)
    if ingredients == 'quit':
        break

输入pig和quit

quit后仍然打印quit

这个点在于增加了ingredients=“”,让代码先运行起来,while后接了一个判断,用while来进行判断,判断后,选择可以输入,打印,如果不符合,那么推出,这里的while是存在一个判断的,这个判断是等于和不等于,也是二元判断,后面改为true false也是,只是条件多了以后,while不方便用while1 while2,就改成if先判断是true还是false,也有点像嵌套。

不太理解,为什么这里我还是会打印quit,也就是while只判断是否等于或不等于,控制运行与否的操作,但是打印都是默认进行的。

而如果要控制是否打印,还需要再通过if判断是否执行。

改进如下:

prompt = "\n Enter the ingredients of pizza"
prompt += '(Enter "quit" when there is no other ingredients)'
ingredients = ""
while ingredients != 'quit':
    ingredients = input(prompt)
    if ingredients != 'quit':
        print(ingredients)
    else:
        break

 这里的quit不被打印了。

下面引入标志,默认while就是一直运行,只有说停才会停。

true就是运行,false就是停→用标志 active来判断

true的状态下,配料等于输入

进而判断各种情况是true还是false→用if

if中通过比较,列举false的情况,

prompt = "\n Enter the ingredients of pizza"
prompt += '(Enter "quit" when there is no other ingredients)'
active = True
while active:
    ingredients = input(prompt)
    
    if ingredients == 'quit':
        active = False
        
    else:
        print(f"add {ingredients}")

下面这个更加简化,active简化了,直接用true false,false直接就break了 

prompt = "\n Enter the ingredients of pizza"
prompt += '(Enter "quit" when there is no other ingredients)'
while True:
    ingredients = input(prompt)
    
    if ingredients == 'quit':
        break
    
    else:
        print(f"add {ingredients}")

练习7-5

练习7-5:电影票 有家电影院根据观众的年龄收取不同的票价:不到3岁的观众免费;3~12岁的观众收费10美元;超过12岁的观众收费15美元。请编写一个循环,在其中询问用户的年龄,并指出其票价。

prompt = "Hello, there is bonus when you pay for the film"
prompt += "enter your age, I'll tell you the price!"
age = ""
while age != "quit":
    age = input(prompt)
    price = int(age)
    if price > 12:
        print(f"you pay for $15")
    elif price > 3:
        print(f"you pay for $12")
    else:
        print(f"free")

 这部分难点是,刚开始会使用while直接判断 age与数字比大小,发现比不了,我尝试了  

age=int(“”)和int(age)>12 都报错

遵循判断二元,那就是等于和不等于,不等于quit后再判断不同的年龄。得到如下:

下一步解决quit: 

考虑先判断如果等于quit直接就break,如果不是quit,再比较。

prompt = "Hello, there is bonus when you pay for the film"
prompt += "enter your age, I'll tell you the price!"
age = ""
while age != "quit":
    age = input(prompt)

    if age == "quit":
        break
    
    else:
        price = int(age)
        if price > 12:
            print(f"you pay for $15")
        elif price > 3:
            print(f"you pay for $12")
        else:
            print(f"free")

 下面这样写也可以

prompt = "Hello, there is bonus when you pay for the film"
prompt += "enter your age, I'll tell you the price!"
age =""
while age != "quit":
    age = input(prompt)

    if age != "quit":
        price = int(age)
        if price > 12:
            print(f"you pay for $15")
        elif price > 3:
            print(f"you pay for $12")
        else:
            print(f"free")

prompt = "Hello, there is bonus when you pay for the film"
prompt += "enter your age, I'll tell you the price!"
actvie = True
while True:
    age = input(prompt)

    if age == "quit":
        active = False
        print("would you like to see a film?")

    else:
        price = int(age)
        if price > 12:
            print(f"you pay for $15")
        elif price > 3:
            print(f"you pay for $12")
        else:
            print(f"free")

这个为什么没有及时停止呢?又重新开始了  搞不懂

因为它还在继续,我在quit后面增加了一个yes和no,如果是yes还会继续,如果是no就真的停止了~哈哈哈这不就是每次退出都不直接退出,疯狂挽留你吗?

prompt = "Hello, there is bonus when you pay for the film"
prompt += "enter your age, I'll tell you the price!"
active = True
while active:
    age = input(prompt)

    if age == "quit":
        print("would you like to see a film?enter yes or no")
        enter = input()
        if enter == "yes":
            continue
        else:
            active = False
    
    else:
        price = int(age)
        if price > 12:
            print(f"you pay for $15")
        elif price > 3:
            print(f"you pay for $12")
        else:
            print(f"free")

 👇尝试了2个新增内容,

1.如果选择退出,继续询问,以挽留

2.如果输入的既不是数字,也不是quit,这一步我不会了,这里的判断不知道该如何处理,如果要进行数字判断,必须要用int,不会了这步,以后想到再加上

prompt = "Hello, there is bonus when you pay for the film"
prompt += "enter your age, I'll tell you the price!"
while True:
    age = input(prompt)

    if age != "quit":
        price = int(age)
        if price < 3:
            print(f"free")
        elif price <= 12:
            print(f"you pay for $12")
        elif price > 12:
            print(f"you pay for $15")
            
        else:
            print("maybe you need to re-enter it")
            continue          
        
            
    elif age == "quit":
        print("would you like to see a film?enter yes or no")
        enter = input()
        if enter == "yes":
            continue
        else:
            break

总结&问题:

 人生如程序,都是选择,选择套选择。优化程序的方式就是做ab二元判断,一个一个拆解成更简单的判断,怪不得程序是二进制。

怎么说呢,二元判断最简单也最难。

感觉学习很慢,几行代码不知道自己的思考和理解是不是对的,虽然程序运行上了,不知道是不是最优解,按我的理解来说,学习最优解的思维方式是比较好的。

写最后一个代码的时候,感觉while还是没有充分理解,且练且学。

  • 27
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值