input python3 int 报错_Python从int(input())异常中检索字符串输入

这是从传递的错误消息中读取输入的hack。它可以正确处理输入字符串中的引号和反斜杠。在#We get input from the user

try:

x = int(input())

except ValueError as var:

#we need to find the text between the quotes in the error message

#but we don't know what kind of quote it will be. We will look for

#the first quote which will be the kind of quotes.

#get the location or existence of each kind of quote

first_dquote = str(var).find('"')

first_squote = str(var).find("'")

used_quote = 0

#if double quotes don't exist then it must be a single quote

if -1 == first_dquote:

used_quote = first_squote

#if single quotes don't exist then it must be a dubble quote

elif -1 == first_squote:

used_quote = first_dquote

#if they both exist then the first one is the outside quote

else: used_quote = min(first_squote,first_dquote)

#the output is what is between the quotes. We leave of the end

#because there is the end quote.

output = str(var)[used_quote+1:-1]

#but the error message is escaped so we need to unescape it

output = bytes(output,"utf_8").decode("unicode_escape")

#print the output

print(output)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值