ارتباط دادن دو لیست با یکدیگر - هفت خط کد انجمن پرسش و پاسخ برنامه نویسی

ارتباط دادن دو لیست با یکدیگر

0 امتیاز

قسمت remove و قسمت replace  باگ دارد

به این شکل که:اگر درسی حذف بشه و یا جالیگزین بشه واحد اون درس تغییری نمیکنه در لیست دانشجو

courses = ['math', 'physics', 'network', 'programming', 'language']
units = [3, 2, 3, 3, 2]
stList = []
stUnit = 0
print('Hi students,wellcome')
print('list of cuorses with units is:')
while True:        
    print('math <3 unit>___physics <2 unit>___network <3 unit>___programing <3 unit>___language <2 unit>') 
    print('your chosen courses: ', stList ,' your chosen unit: ', stUnit)
    choose = input('enter the courses you want to select, < you dont let to choose more than 10 units>,enter (e) for exit :' ) 
    while choose in stList:
        choose = input('Repetitive course  try againe: ')
        if choose == 'e':
            break       
    if choose == 'e':
        break    
    if choose in courses:
        index = courses.index(choose)          
        if stUnit + units[index] > 10:
            print('you dont let to choose more than 10 units  <try againe>')
        else:
            stList.append(choose)
            stUnit += units[index]       
    else:
        print('invalid entery,your choose is out of the list  <try againe>')         
print('your choised list of courses is: ',stList)
print('your choised list of units is: ',stUnit)
while True:
    if input('for finish enter <f>, else if you want chenge the list (add,remove,replace) enter another key: ') == 'f' :
       break 
    else:
        print('your chosen courses: ', stList ,' your chosen unit: ', stUnit)
        change = input('for change,enter the number of option>>>  1)add____2)remove___3)replace : ')        
        if change == '1':
            add = input('enter the course you want to add: ')
            while add in stList:
                add = input('Repetitive course  try againe  <enter (e) for exit> : ')
                if add == 'e':
                    break
            if add in courses:
                index = courses.index(add)          
                if stUnit + units[index] > 10:
                    print('you dont let to choose more than 10 units  <try againe>')
                else:
                    stList.append(add)
                    stUnit += units[index]   
            else:
                print('invalid entery,your choose is out of the list  <try againe>')                        
        elif change == '2':
            rem = input('enter the course you want to remove: ')            
            if rem in stList:                             
                stList.remove(rem)                  
            else:
                print('invalid entery,your choose is out of the list  <try againe>')                        
        elif change == '3':
            replace1 = input('enter your course of list that you want change: ')
            if replace1 in stList:                
                 replace2 = input(f'enter your course  that you want replace with {replace1}: ')                                  
                 while replace2 in stList:
                     replace2 = input('Repetitive course  try againe  <enter (e) for exit> : ')
                     if replace2 == 'e':
                        break
                 if replace2 in courses:  
                   index = courses.index(replace2)          
                   if stUnit + units[index] > 10:
                       print('you dont let to choose more than 10 units  <try againe>')
                   else:
                      stList = list(map(lambda x:x.replace(replace1,replace2), stList))
                      stUnit += units[index] 
                 else:
                     print('invalid entery,your choose is out of the list  <try againe>')
            else:
                print('invalid entery,your choose is out of the list  <try againe>')
        else:
            print('invalid entery,your choose is out of the list  <try againe>' )
print('your choised list of courses is: ',stList)
print('your choised list of units is: ',stUnit)   

      
input()

 

 

سوال شده مهر 20, 1402 بوسیله ی بی نام  
ویرایش شده مهر 20, 1402 بوسیله ی مصطفی ساتکی

1 پاسخ

0 امتیاز
 
بهترین پاسخ

با توجه به کدی که ارائه دادید، مشکل در قسمت حذف و جایگزینی دروس وجود دارد. وقتی یک درس حذف یا جایگزین می‌شود، واحد آن درس در لیست دانشجو تغییر نمی‌کند. برای حل این مشکل، شما باید وقتی یک درس حذف یا جایگزین می‌شود، واحد آن را نیز از مجموع واحدهای انتخاب شده کم یا اضافه کنید.

برای مثال، در قسمت حذف:

elif change == '2':
    rem = input('enter the course you want to remove: ')            
    if rem in stList:
        index = courses.index(rem)
        stUnit -= units[index] # subtract the units of the removed course
        stList.remove(rem)                  
    else:
        print('invalid entery,your choose is out of the list  <try againe>')  

و در قسمت جایگزینی:

elif change == '3':
    replace1 = input('enter your course of list that you want change: ')
    if replace1 in stList:                
         replace2 = input(f'enter your course  that you want replace with {replace1}: ')                                  
         while replace2 in stList:
             replace2 = input('Repetitive course  try againe  <enter (e) for exit> : ')
             if replace2 == 'e':
                break
         if replace2 in courses:  
           index1 = courses.index(replace1)
           index2 = courses.index(replace2)
           if stUnit - units[index1] + units[index2] > 10:
               print('you dont let to choose more than 10 units  <try againe>')
           else:
              stList = list(map(lambda x:x.replace(replace1,replace2), stList))
              stUnit = stUnit - units[index1] + units[index2] # update the total units
         else:
             print('invalid entery,your choose is out of the list  <try againe>') 

 

 

پاسخ داده شده مهر 20, 1402  بوسیله ی Lori3 (امتیاز 857)   11 36 105
...