Skip to main content
 首页 » 编程设计

Python 问题 : Try and Excepts with if

2025年05月04日24linjiqin

这可能是非常基本的,但如果 cointype() 不在字典 coin_int 中,我试图触发 Except 运行,但是它直接跳出 if 条件而不使用 Except 即使遇到值错误? 感谢您的帮助。

try: 
    coin_type = input("Input your coin: 1p, 2p, 5p etc...  ") 
    if coin_type in coin_int: 
        print("That value is recognised inside the list of known coin types") 
 
except ValueError: 
    print("That value of coin is not accepted, restarting...") 

请您参考如下方法:

您想引发一个异常。只是

raise ValueError("wrong coin type")