鱼C论坛

 找回密码
 立即注册
查看: 1307|回复: 7

[已解决]while循环问题

[复制链接]
发表于 2017-11-13 11:44:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
ages = 'Please enter your age: '
ages += "\nEnter 'quit' to end the program "
name = ''
while name != 'quit':
    name = int(input(ages))
    if name < 3 :
        print('Less than 3 years old free')
    elif name <= 12 :
        print('It costs 10 dollars')
    else:
        print('It costs 15 dollars')

Please enter your age:
Enter 'quit' to end the program 1
Less than 3 years old free
Please enter your age:
Enter 'quit' to end the program -5
Less than 3 years old free
Please enter your age:
Enter 'quit' to end the program

问题:我输入数值后,怎么还打印要我输入数值,这个怎么结束:还有一个问题,我输入-5也会打印免费,这就是个漏洞,这个该怎么办?
最佳答案
2017-11-13 13:47:47
pythonwei 发表于 2017-11-13 13:37
ages = 'Please enter your age: '
ages += "\nEnter 'quit' to end the program: "
active = True

出现错误的原因不是你的逻辑,而是当你输入字符串的时候,不能对字符串用int强制类型转换
比如
a = 'good'
b = int(a) #这样就是不对的,因为无法将一个非数字的str转换为一个数字

所以你看我的代码,如果输入了字符串,那么会用continue语句跳过本次循环后面的内容,也就不会进行上面的int强制转换了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-11-13 11:50:59 | 显示全部楼层
  1. ages = 'Please enter your age: '
  2. ages += "\nEnter '-1' to end the program "
  3. name = ''
  4. while True:
  5.     name = int(input(ages))
  6.     if name == -1:
  7.         break
  8.     if name <= 0:
  9.         print('Error!')
  10.     elif name < 3 :
  11.         print('Less than 3 years old free')
  12.     elif name <= 12 :
  13.         print('It costs 10 dollars')
  14.     else:
  15.         print('It costs 15 dollars')
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-11-13 11:58:16 | 显示全部楼层
我输入数值后,怎么还打印要我输入数值,这个怎么结束
你的while循环的判断条件要求name == 'quit'
但是第一句你家将输入进行了int转换,也就是name永远不可能是字符串'quit'
while成了死循环,应该用if判断输入,再进行转换

还有一个问题,我输入-5也会打印免费,这就是个漏洞,这个该怎么办?
对输入的数据进行判断,负数则提示错误

修改如下:
  1. ages = 'Please enter your age'
  2. ages += "\nEnter 'quit' to end the program :"
  3. name = ''
  4. while name != 'quit':
  5.     name = input(ages)
  6.     if name == 'quit':
  7.         continue #或者用break
  8.    
  9.     name = int(name)
  10.     if name < 0:
  11.         print('error!')
  12.     elif name < 3 :
  13.         print('Less than 3 years old free')
  14.     elif name <= 12 :
  15.         print('It costs 10 dollars')
  16.     else:
  17.         print('It costs 15 dollars')
复制代码


结果:
  1. Please enter your age
  2. Enter 'quit' to end the program :-3
  3. error!
  4. Please enter your age
  5. Enter 'quit' to end the program :2
  6. Less than 3 years old free
  7. Please enter your age
  8. Enter 'quit' to end the program :6
  9. It costs 10 dollars
  10. Please enter your age
  11. Enter 'quit' to end the program :18
  12. It costs 15 dollars
  13. Please enter your age
  14. Enter 'quit' to end the program :quit
  15. >>>
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-11-13 12:10:21 | 显示全部楼层

为什么要打印
if name  == -1:     它和 if name <= 0 :   有什么不同?

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-11-13 13:37:07 | 显示全部楼层
BngThea 发表于 2017-11-13 11:58
我输入数值后,怎么还打印要我输入数值,这个怎么结束
你的while循环的判断条件要求name == 'quit'
但是 ...

ages = 'Please enter your age: '
ages += "\nEnter 'quit' to end the program: "
active = True
while active:
    message = input(ages)
    if message == 'quit' :
        active = False
    else:
        print(message)
      
    message = int(message)
    if message < 3 :  
        print('Less than 3 years old free')
    elif message <= 12 :
        print('It costs 10 dollars')
    else:
        print('It costs 15 dollars')

RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python36/jk.py
Please enter your age:
Enter 'quit' to end the program: quit
Traceback (most recent call last):
  File "C:/Users/Administrator/AppData/Local/Programs/Python/Python36/jk.py", line 11, in <module>
    message = int(message)
ValueError: invalid literal for int() with base 10: 'quit'
问题:这样不行吗?如果是'quit'已经在上一个if条件中结束结束循环了啊,我已经设变量active = False了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-11-13 13:37:52 | 显示全部楼层
pythonwei 发表于 2017-11-13 12:10
为什么要打印
if name  == -1:     它和 if name

<=0都是无效输入,但特意把-1定义为退出循环,所以单独写一个if
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-11-13 13:47:47 | 显示全部楼层    本楼为最佳答案   
pythonwei 发表于 2017-11-13 13:37
ages = 'Please enter your age: '
ages += "\nEnter 'quit' to end the program: "
active = True

出现错误的原因不是你的逻辑,而是当你输入字符串的时候,不能对字符串用int强制类型转换
比如
a = 'good'
b = int(a) #这样就是不对的,因为无法将一个非数字的str转换为一个数字

所以你看我的代码,如果输入了字符串,那么会用continue语句跳过本次循环后面的内容,也就不会进行上面的int强制转换了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-11-13 14:11:48 | 显示全部楼层
BngThea 发表于 2017-11-13 13:47
出现错误的原因不是你的逻辑,而是当你输入字符串的时候,不能对字符串用int强制类型转换
比如
a = 'go ...

谢谢   我懂了!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 21:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表