鱼C论坛

 找回密码
 立即注册
查看: 602|回复: 9

今天的python习题

[复制链接]
发表于 2018-8-31 16:59:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1、第一次满足条件了,又想调试一下,调试最后出现了错误,都不知道哪里错了:
代码:
def strrange(stop,begin="A",step=1):

    # 初始化
    list_temp = []
    list_alphabet = []
    str_temp = ""
    count_begin = -1
    global str_alphabet

    # 字符串中是否含有起始字符或A
    while ("A" not in str_alphabet) and (begin not in str_alphabet):
        str_alphabet = input("字符串输入有误,请重新输入:")

    # 字符串中开始后是否存在终止符
    while stop not in str_alphabet:
        stop = input("字符串输入有误,不包含终止符,请重新输入:")

    for each_alphabet in str_alphabet:
        list_temp.append(each_alphabet)
       
    # 判断起始位置
    for i in range(len(list_temp)):
        count_begin += 1
        if list_temp[i] == begin:
            break
        
    # 判断结束位置
    count_stop = count_begin-1
    for j in range(count_begin,len(list_temp)):
        count_stop += 1
        if list_temp[i] == stop:
            break
    
    # 按照步伐提取字符串
    for each_step in range(count_begin,count_stop,step):
        list_alphabet.append(list_temp[each_step])

    for each in list_alphabet:
        if each.isalpha():
            str_temp += each
    return str_temp
            

str_alphabet = input("请输入你想说的话:")
        

问题是:
请输入你想说的话:Afklguberhiopkl'
>>> strrange("i")
'Afklguberhiopkl'
>>>
提取不出来,哭了!
2、这是最初调试的,想调用外部函数内部内容,忘了不知道怎么办?
代码:
def strrange(stop,begin="A",step=1):

    # 初始化
    list_alphabet = []
    str_temp = ""

    # 判断输入是否有错误
    judge(begin,stop,str_alphabet)
      
    # 判断起始位置
    for i in range(len(list_temp)):
        count_begin += 1
        if list_temp[i] == begin:
            break
        
    # 判断结束位置
    count_stop = count_begin
    for j in range(count_begin,len(list_temp)):
        count_stop += 1
        if list_temp[i] == stop:
            break
        
    # 按照步伐提取字符串
    for each_step in range(count_begin,(count_stop),step):
        list_alphabet.append(list_temp[each_step])

    for each in list_alphabet:
        if each.isalpha():
            str_temp += each
    return str_temp
            

str_alphabet = input("请输入你想说的话:")


def judge(begin,stop,str_alphabet):
    Brk = 1
    count_begin = -1
    list_temp = []
    list_temp_Brk = []
    # 字符串中是否含有起始字符或A
    while ("A" not in str_alphabet) and (begin not in str_alphabet):
        str_alphabet = input("字符串输入有误,请重新输入:")

    # 字符串中开始后是否存在终止符
    while stop not in str_alphabet:
        str_alphabet = input("字符串输入有误,不包含终止符,请重新输入:")
    for each_alphabet in str_alphabet:
        list_temp.append(each_alphabet)
   
    # 判断起始位置与结束位置
    for i in range(len(list_temp)):
        count_begin += 1
        if list_temp[i] == begin:
            break
    for j in range(count_begin,len(list_temp)):
        list_temp_Brk.append(list_temp[j])
    while stop not in list_temp_Brk:
        stop = input("终止符输入错误,请重新输入:")
               
    return stop,list_temp,count_begin      



问题:

>>> strrange("u")
Traceback (most recent call last):
  File "<pyshell#32>", line 1, in <module>
    strrange("u")
  File "C:\python\3.其他\每日练习\204strrange.py", line 11, in strrange
    for i in range(len(list_temp)):
NameError: name 'list_temp' is not defined
各位大哥帮下忙!!!!!


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-8-31 16:59:58 | 显示全部楼层
def strrange(stop,begin="A",step=1):

    # 初始化
    list_alphabet = []
    str_temp = ""

    # 判断输入是否有错误
    judge(begin,stop,str_alphabet)
       
    # 判断起始位置
    for i in range(len(list_temp)):
        count_begin += 1
        if list_temp[i] == begin:
            break
        
    # 判断结束位置
    count_stop = count_begin
    for j in range(count_begin,len(list_temp)):
        count_stop += 1
        if list_temp[i] == stop:
            break
        
    # 按照步伐提取字符串
    for each_step in range(count_begin,(count_stop),step):
        list_alphabet.append(list_temp[each_step])

    for each in list_alphabet:
        if each.isalpha():
            str_temp += each
    return str_temp
            

str_alphabet = input("请输入你想说的话:")


def judge(begin,stop,str_alphabet):
    Brk = 1
    count_begin = -1
    list_temp = []
    list_temp_Brk = []
    # 字符串中是否含有起始字符或A
    while ("A" not in str_alphabet) and (begin not in str_alphabet):
        str_alphabet = input("字符串输入有误,请重新输入:")

    # 字符串中开始后是否存在终止符
    while stop not in str_alphabet:
        str_alphabet = input("字符串输入有误,不包含终止符,请重新输入:")
    for each_alphabet in str_alphabet:
        list_temp.append(each_alphabet)
    
    # 判断起始位置与结束位置
    for i in range(len(list_temp)):
        count_begin += 1
        if list_temp[i] == begin:
            break
    for j in range(count_begin,len(list_temp)):
        list_temp_Brk.append(list_temp[j])
    while stop not in list_temp_Brk:
        stop = input("终止符输入错误,请重新输入:")
                
    return stop,list_temp,count_begin      
第2个问题的代码!!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-31 17:04:01 | 显示全部楼层
我运行没提示你这个错误,你怎么还用斜体啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-31 17:05:49 | 显示全部楼层
还有为啥我想说什么话的输入
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-31 17:33:57 | 显示全部楼层
Chysial 发表于 2018-8-31 16:59
第2个问题的代码!!!!

第二个list_temp没有,
上面初始化那里添加就好了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-31 21:38:48 | 显示全部楼层
本帖最后由 凌九霄 于 2018-8-31 22:22 编辑

简单问题复杂化了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-9-1 05:56:43 | 显示全部楼层
塔利班 发表于 2018-8-31 17:04
我运行没提示你这个错误,你怎么还用斜体啊

斜体是直接复制,没点代码的原因,竟然换成佐为的头像了,和你身份不符
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-9-1 05:58:08 | 显示全部楼层
凌九霄 发表于 2018-8-31 21:38
简单问题复杂化了

我只想变得完美一点而已
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-9-1 06:22:16 | 显示全部楼层
拉了盏灯 发表于 2018-8-31 17:33
第二个list_temp没有,
上面初始化那里添加就好了。

我上面添加,会提示没有对list_temp定义
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-1 06:59:52 | 显示全部楼层
Chysial 发表于 2018-9-1 05:58
我只想变得完美一点而已

完美?有什么特点?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-6 20:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表