خانه › انجمن ها › انجمن برنامه نویسی › انجمن برنامه نویسی پایتون › لیست اعداد › پاسخ به: لیست اعداد
اکتبر 27, 2023 در 1:40 ب.ظ
#3052
::
` def calculator(n, m, li): print("Your list: ", li) for i in range(0, n//m): z1 = sum(li[0 + i*m : (i+1)*m]) x.append(z1) if n%m != 0: z2 = sum(li[(n//m)*m : ]) x.append(z2) print("list of summations: ", x) z3 = len(x) res = 0 for i in range(0, z3): res += ((-1)**i) * x[i] print("List's final value: ", res) flag = True while(flag): x = [] n = int(input("Enter n: ")) m = int(input("Enter m: ")) li = list(map(int, input("Enter your list numbers separated by space: ").split())) if n < 2: print("n (lenght of list) must be at least 2. Try again! ") flag = False elif m >= n: print("m must be less than n. Try again!") flag = False elif len(li) != n: print("Please enter exactly ", n, " elements for your list, separated by 1 space. Try again!") flag = False else: calculator(n, m, li) if flag == True: resp = input("Do you want to continue (Yes or No)?: ") if resp.upper() == "NO" or resp.upper() == "N": flag = False `
- این پاسخ 1 سال، 1 ماه پیش توسط ابوالفضل محمدی جو اصلاح شده است.