haimianxx 2022-05-29 20:15 采纳率: 86.2%
浏览 21
已结题

这个python问题该怎么解决啊?

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果

img

def text_index(string,index):
    print(string[index])

str_x = ''
num = 0
try:
    str_x = pass
    num = pass
    text_index(str_x,num)
    str_x + num
except:
    pass
else:
   pass
finally:
   pass

  • 写回答

1条回答 默认 最新

  • 请叫我问哥 Python领域新星创作者 2022-05-29 22:22
    关注
    def text_index(string,index):
        print(string[index])
     
    str_x = ''
    num = 0
    try:
        str_x = input()
        num = int(input())
        text_index(str_x,num)
        str_x + num
    except Exception as e:
        print(repr(e))
    

    主要是这几句,测试的时候依次下面三种输入,都会报错

    abc
    a
    ValueError("invalid literal for int() with base 10: 'a'")
    
    abc
    3
    IndexError('string index out of range')
    
    abc
    2
    c
    TypeError('can only concatenate str (not "int") to str')
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 6月6日
  • 已采纳回答 5月29日
  • 创建了问题 5月29日