微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Python 中使用除了 AssertionError 之外的 while 循环继续工作

如何解决如何在 Python 中使用除了 AssertionError 之外的 while 循环继续工作

大家好,我需要一些帮助。

我正在做这个练习。我需要什么,正在发生什么?

当我进入 except ValueError 循环回到开始时,我不一定需要写 continue 到它发生,好的(这是什么欲望)。

但是当我进入除了 AssertionError 时,不管我是否写入 continue,循环就会停止,这就是问题所在。当我进入 except AssertionError 时,我需要循环回到开头。

你能帮我吗?

prompt="\nWould you like to get a ticket?"  
prompt+="\nPlease write your age: "

age= 0    
while age >= 0:


try:       
    age=int(input(prompt))
    assert age > 0             
  
except ValueError:
    print(f"This option is not valid. You need to input an integer number. Try it again.")
   
    
except AssertionError: 
    print("Wow that can't be possible!! Try it again")
    continue
        
           
else:      
    
                
    if age >= 0 and age <3:
        print(f"You are {age} years old,your ticket is free!")
        break        
        
    elif age >=3 and age<=12:
        print(f"You are {age} years old,your ticket price is $10.")
        break 
        
    elif age > 75 and age <121:        
        print(f"You are {age} years old,your ticket price is $7.")
        break
   
    else:
        print(f"You are {age} years old,your ticket price is $15.")
        break
  
finally:
    print("Fim da execução")

==============

输出

要买票吗?
请填写您的年龄:一
此选项无效。您需要输入一个整数。再试一次。
执行程序

要买票吗?
请填写您的年龄:-45
哇,这不可能!!再试试
执行程序

==============

我的循环到此停止
但我需要它再问我一次:

你想要一张票吗?
请填写您的年龄:

解决方法

您的 while 循环终止条件是倒退的,您对 finally 的使用似乎不合适,而且您确实需要 continue 块后面的 except 语句。但是,总体而言,您非常接近。下面的示例修复了您遇到的问题,但尝试使整体逻辑与您所​​遇到的保持相同。

编辑:意识到您在成功处理每个案例后都使用了 break,因此无需使用基于年龄的 while 循环条件。

示例:

prompt="\nWould you like to get a ticket?"
prompt+="\nPlease write your age: "

while True:

    try:       
        age = int(input(prompt))
        assert age > 0             
  
    except ValueError:
        print(f"This option is not valid. You need to input an integer number. Try it again.")
        continue

    except AssertionError: 
        print("Wow that can't be possible!! Try it again")
        continue
                    
    if age >= 0 and age <3:
        print(f"You are {age} years old,your ticket is free!")
        break        
        
    elif age >=3 and age<=12:
        print(f"You are {age} years old,your ticket price is $10.")
        break 
        
    elif age > 75 and age <121:        
        print(f"You are {age} years old,your ticket price is $7.")
        break
   
    else:
        print(f"You are {age} years old,your ticket price is $15.")
        break
  
print("Fim da execução")

输出:

Would you like to get a ticket?
Please write your age:  one
This option is not valid. You need to input an integer number. Try it again.

Would you like to get a ticket?
Please write your age:  -45
Wow that can't be possible!! Try it again

Would you like to get a ticket?
Please write your age:  10
You are 10 years old,your ticket price is $10.
Fim da execução

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。