python中valueerror_如何在python中修复我的ValueError消息?

1586010002-jmsa.png

choice1=("yes")

choice=("no")

import sys

while True:

try:

user5=input("is your phone working")

except ValueError:

print("Sorry, I didn't understand that, make sure to reply with 'YES' or 'NO'")

if user5==choice1:

print("thats great")

sys.exit(0)

break

elif user5==choice:

print("you could contact seller")

while True:

try:

user_response=input("is it fix?")

except ValueError:

print("sorry i didnt understand that, make sure to reply with 'YES' or 'NO'")

if user_response==choice1:

print("great")

sys.exit(0)

break

elif user_response==choice:

break

break

When the user gives an invalid response, its suppose to print the error message but it skips it, and repeats the question. Like so:

is your phone workinglll

is your phone working;';'';

is your phone workinglll;l;l

is your phone working;ll;';';l;';'

is your phone working

When the user enters an invalid response, its suppose to display the message

:sorry I didn't understand that, make sure to reply with 'YES' or 'NO and then ask the question again until it gets a valid response.

解决方案input just accepts any string, it won't throw a ValueError if the response is anything else than yes or no.

Instead, you have to use an if-else statement that checks that you responded yes or no, and if you didn't, the continue statement returns to the beginning of the loop so you get the question again.

user5 = input("Is your phone working?").lower()

if user5 != choice and user5 != choice1:

print("Sorry, I didn't understand that, make sure to reply with 'YES' or 'NO'.")

continue

And:

user_response = input("Is it fixed?").lower()

if user_response != choice and user_response != choice1:

print("Sorry, I didn't understand that, make sure to reply with 'YES' or 'NO'.")

continue

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值