Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fix Error and clenup code
#1
Hi

I would like someone help with my python code.
What I need:
1. Solve error
2. Change code to something more clear and organized

I use Linux Mint 19 Cinnamon and Python 3

I try enter with 2162 for Player A and 1600 for Player B, but error...
Quote:Enter Rating of Player A:
2162
Enter Rating of Player B:
1600

rst: 560
Traceback (most recent call last):
File "bkp_return_pe.py", line 39, in <module>
proc = diff_rating_str(Rt_A,Rt_B)
NameError: name 'diff_rating_str' is not define
[attachment=513]

import csv


Rt_A = int(input("Enter Rating of Player A:\n"))
Rt_B = int(input("Enter Rating of Player B:\n"))


def diff_rating(Rt_A, Rt_B):
    # Retorna a diferença entre 2 Ratings  
    return abs(Rt_A-Rt_B)


# Dados de um arquivo csv dentro de uma lista

tab_list = [0, 4, 11, 18, 26, 33, 40, 47, 54, 62, 69, 77, 84, 92, 99, 107,
            114, 122, 130, 138, 146, 154, 163, 171, 180, 189, 198, 207, 216,
            226, 236, 246, 257, 268, 279, 291, 303, 316, 329, 345, 358, 375,
            392, 412, 433, 457, 485, 518, 560, 620, 735]


# Pega o valor referente a diferena dos Ratings
value_chosen = diff_rating(Rt_A,Rt_B)
# Compara com o mais proximo caso não seja exato

rst = min(tab_list, key=lambda x:abs(x-value_chosen))
print("\nrst: ", rst)

# Verifica e ajusta o mais proximo caso [rst] seja maior
if rst > value_chosen:
    # rst = next((x for x in tab_list if x > rst), None)
    n = rst
    ind = tab_list.index(n)
    resultt = tab_list[ind-1]
    rst = resultt
    print("\nrst-resullt: ", rst)


# Pega o valor referente a diferena dos Ratings
proc = diff_rating_str(Rt_A,Rt_B)
print("proc: ",proc)
print("\nDiferença dos Ratings ->: ",value_chosen)


def diff_rating_str(Rt_A, Rt_B):
    # Tranposição da dif de rating para string
    # para facilitar a verificação na a coluna (4)
    if abs(Rt_B-Rt_A) < 4:
        result_val = "0Equal"
    else:
        if Rt_A < Rt_B:
            result_val = str(resultt) + "Min"
        elif Rt_A > Rt_B:
            result_val = str(resultt) + "Large"
        else:
            result_val = str(resultt) + "Equal"
    return result_val


# Faz a leitura do arquivo CSV e retorna a coluna 4
with open('rating_tab.csv') as f:
    ler = csv.reader(f)
    for row in ler:
        if proc == row[3]:

            print("Encontrado: ", row[1])
Reply
#2

  1. Don't use function diff_rating_str() (line 39) before it is defined (line 44)
  2. Use built-in module bisect to compute rst. Write more functions and less code outside of functions.
Reply
#3
Hi Gribouillis

First of all, thanks.
Sorry about insert code in wrong tags.
I try bisect, but is not work as expected.
import bisect

'''
ex1:
If function diff_rating, return any value 0...3, I need get value 0 inside tab_list

ex2:
If function diff_rating, return any value 4...10, I need get value 4 inside tab_list

ex3:
If function diff_rating, return any value 33...39, I need get value 33 inside tab_list

ex4:
If function diff_rating, return any value 138...145, I need get value 138 inside tab_list

so on...
'''

tab_list = [0, 4, 11, 18, 26, 33, 40, 47, 54, 62, 69, 77, 84, 92, 99, 107,
            114, 122, 130, 138, 146, 154, 163, 171, 180, 189, 198, 207, 216,
            226, 236, 246, 257, 268, 279, 291, 303, 316, 329, 345, 358, 375,
            392, 412, 433, 457, 485, 518, 560, 620, 735]
# I need find in tab_list exact or approximately match based return in function diff_rating  
print(bisect.bisect_left(tab_list, 4)) # Return 1, ok!
print(bisect.bisect_left(tab_list, 5)) # Return 2, but I need return 1
print(bisect.bisect_left(tab_list, 6)) # Return 2, but I need return 1
print(bisect.bisect_left(tab_list, 7)) # Return 2, but I need return 1
print(bisect.bisect_left(tab_list, 8)) # Return 2, but I need return 1
print(bisect.bisect_left(tab_list, 9)) # Return 2, but I need return 1
print(bisect.bisect_left(tab_list, 10)) # Return 2, but I need return 1
Reply
#4
I suggest this
import bisect
tab_list = [0, 4, 11, 18, 26, 33, 40, 47, 54, 62, 69, 77, 84, 92, 99, 107,
            114, 122, 130, 138, 146, 154, 163, 171, 180, 189, 198, 207, 216,
            226, 236, 246, 257, 268, 279, 291, 303, 316, 329, 345, 358, 375,
            392, 412, 433, 457, 485, 518, 560, 620, 735]


def get_rst(value):
    """computes the largest number in tab_list <= value. Works if tab_list[0] <= value"""
    return tab_list[bisect.bisect_right(tab_list, value) - 1]
 

# test this
for i in range(20):
    print(i, get_rst(i))
Output:
0 0 1 0 2 0 3 0 4 4 5 4 6 4 7 4 8 4 9 4 10 4 11 11 12 11 13 11 14 11 15 11 16 11 17 11 18 18 19 18
Reply
#5
See my edits above, with bisect_right() which is better here.
Reply
#6
[SOLVED]
I can't believe that solved this using a few lines of code!
Very good!!

Thank you!!!!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020