weixin_57733352
2021-06-17 16:51
采纳率: 100%
浏览 53

python中一个变量的一个调用

# -*- coding: gb2312 -*-


class studentApp:
    def __init__(self, user): 
        self.inputNum=0
        self.interface = user        
        
    def run(self):
        while True:
            number,sNo,sName,sAge,sScore,sPhone,sEmail= self.interface.getSInfo()    
            student_info=read_info()
            #print(student_info)
            sinfo = {"sno":sNo,"name":sName,"age":sAge,"score":sScore,"phone":sPhone,"Email":sEmail} 
            #print(sinfo)
            if number == '1':
                student_info=read_info()
            elif number == '2':               
                print('---Students---',student_info)
                self.interface.showChart(student_info) 
            elif number == '3':
                mod_info(sinfo,student_info)
            elif number == '4':
                save_info(student_info)                  
            else:
                break            
            self.interface.show_student_info(student_info)  
            self.interface.number.set('0') 
      

def read_info():
    old_info = []
    try:
        students_txt = open("grade.txt")
    except:
        print("No information")                       
        return
    while True:
        info = students_txt.readline()
        if not info:
            break
        
        info = info.rstrip()    
        info = info[1:-1]       
         
        student_dict = {}       
        for x in info.split(","):   
            # print(x)
            key_value = []      
            for k in x.split(":"):  
                k = k.strip()       
                # print(k)
                if k[0] == k[-1] and len(k) > 2:        
                    key_value.append(k[1:-1])           
                else:
                    key_value.append(k)
                   # print(key_value)
            student_dict[key_value[0]] = key_value[1]   
        # print(student_dict)
        old_info.append(student_dict)   
    students_txt.close()  
    return old_info   

def mod_info(sinfo,student_info):
    mod_sno = sinfo.get("sno")
   # print(mod_sno)
    for info in student_info:
        if mod_sno == info.get("sno"):
           # print(mod_sno)
            sScore = int(sinfo.get("score"))
            #print(sScore)
            info = {"sno":mod_sno,"name":info.get("name"),"age":info.get("age"),"score":sinfo.get("score"),"phone":info.get("phone"),"Email":info.get("Email")}  
            #print(info)
            
            return info
   

def save_info(student_info):
    try:
        students_txt = open("grade.txt", "w")    
    except Exception as e:
        students_txt = open("grade.txt", "x") 
    
    for a in student_info:
        students_txt.write(str(a) + "\n") 
        print("----"+str(a))      
    students_txt.close()
    

这是我的一个代码,我希望mod_info中的info被save_info中的a得到,该如何调用info,求解?注python版本2.7

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • String name="小吴"; 2021-06-17 20:12
    已采纳

    被a得到是什么意思?

    点赞 打赏 评论
  • String name="小吴"; 2021-06-17 21:28
    def mod_info(sinfo, student_info):
        mod_sno = sinfo.get("sno")
        # print(mod_sno)
        for info in student_info:
            if mod_sno == info.get("sno"):
                # print(mod_sno)
                sScore = int(sinfo.get("score"))
                # print(sScore)
                info = {"sno": mod_sno, "name": info.get("name"), "age": info.get("age"), "score": sinfo.get("score"),
                        "phone": info.get("phone"), "Email": info.get("Email")}
                # print(info)
    
                return info
    
    
    def save_info(student_info):
        try:
            students_txt = open("grade.txt", "w")
        except Exception as e:
            students_txt = open("grade.txt", "x")
    
        for a in student_info:
            students_txt.write(str(a) + "\n")
            print("----" + str(a))
        students_txt.close()
    
        # 你想在哪里用这个变量就在哪里,调用这个函数,然后拿到这个函数的返回值。
        # 这个mod_变量就是info的返回值 ,但是你声明的这个函数mod_info()需要两个参数。你给它两个参数就可以调用这个函数,并得到这个函数的info返回值了
        mod_ = mod_info()
        # 拿到info的返回值以后赋值给mod_ 此时的mod_就是info
        a = mod_
        # 在把mod_赋值给a此时的a就是info
        print(a)
        
        # 明白了吗?
    点赞 打赏 评论
  • 有问必答小助手 2021-06-19 00:02

    您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

    如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

    ps: 问答会员年卡【8折】购 ,限时加赠IT实体书,即可 享受50次 有问必答服务,了解详情>>>https://t.csdnimg.cn/RW5m

    点赞 打赏 评论

相关推荐 更多相似问题