gpt4 book ai didi

Python 问题 : Try and Excepts with if

转载 作者:太空宇宙 更新时间:2023-11-04 08:45:47 24 4
gpt4 key购买 nike

这可能是非常基本的,但如果 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")

关于Python 问题 : Try and Excepts with if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40747862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com