本帖最后由 旺仔牛奶唐 于 2022-12-31 21:57 编辑
怎么将input输入的内容限制在字典的键值之中? def carculate(money,price): result = int(money)-int(price) if int(result)>0: print(f"Send back you {int(result)}") elif int(result)==0: print(f"Thankyou for shopping!") elif int(result)<0: left_money=0-int(result) print(f"Sorry,you need {left_money} more to pay!") active = True while active: prices={"cake1":12,'cake2':24,'cola1':56,'cola2':78,'candy1':34,'candy2':57} price=prices[input(f"Chose your goods:\n").lower()] money=input("Give the money:\n") carculate(money,price) asking=input(f"Have you finihed? anser'yes'or'not'.\n") if asking.lower() == 'yes': print(f"See you next time!") break else: print(f"All depends on you.") continue 在 price=prices[input(f"Chose your goods:\n").lower()]一行,成功把输入内容限制在字典prices的键之内,但是输入字典没有的内容会直接报错报错,如何更改使得如果用户输入不属于字典键的内容时进行提示而不是直接报错?
|