Как получить имя переменной вместо номера в переменной, используя функцию max в python - PullRequest
0 голосов
/ 13 октября 2018

Я пытаюсь получить целые числа из базы данных, распечатать их в этих строках, и в конце распечатки сказать мне, какое целое число является наибольшим и наименьшим.Я использую функции max и min и вместо того, чтобы делать то, что я хочу, это распечатывать наибольшее и наименьшее количество всех переменных вместо распечатки самой переменной.

Код:

import pyodbc


one = 0
two = 0
three = 0
four = 0
five = 0
six = 0
seven = 0
eight = 0
nine = 0
ten = 0
eleven = 0
twelve = 0
thirteen = 0
fourteen = 0
fifteen = 0
sixteen = 0
seventeen = 0
eighteen = 0
nineteen = 0
twenty = 0
twentyone = 0
twentytwo = 0
twentythree = 0
twentyfour = 0
twentyfive = 0
twentysix = 0
twentyseven = 0
twentyeight = 0
twentynine = 0
thirty = 0
thirtyone = 0
thirtytwo = 0 
thirtythree = 0
thirtyfour = 0
thirtyfive = 0
thirtysix = 0
thirtyseven = 0
thirtyeight = 0
thirtynine = 0
fourty = 0
fourtyone = 0
fourtytwo = 0
fourtythree = 0
fourtyfour = 0
fourtyfive = 0 
fourtysix = 0
fourtyseven = 0
fourtyeight = 0
fourtynine = 0
fifty = 0
fiftyone = 0
fiftytwo = 0
fiftythree = 0
fiftyfour = 0
fiftyfive = 0
fiftysix = 0
fiftyseven = 0
fiftyeight = 0
fiftynine = 0
sixty = 0
sixtyone = 0
sixtytwo = 0
sixtythree = 0
sixtyfour = 0 
sixtyfive = 0
sixtysix = 0
sixtyseven = 0
sixtyeight = 0
sixtynine = 0

row_counter = 0





conn = pyodbc.connect(r'DSN=MACCD')
cursor = conn.cursor()
cursor.execute('SELECT first,second,third,fourth,fifth FROM WhiteBalls')

while True:
    row = cursor.fetchone()
    if row is None:
        break

    row_counter += 1
    print(row_counter, row)

    if 1 in row:
        one += 1

    if 2 in row:
        two += 1

    if 3 in row:
        three += 1

    if 4 in row:
        four += 1

    if 5 in row:
        five += 1

    if 6 in row:
        six += 1

    if 7 in row:
        seven += 1

    if 8 in row:
        eight += 1

    if 9 in row:
        nine += 1

    if 10 in row:
        ten += 1

    if 11 in row:
        eleven += 1

    if 12 in row:
        twelve += 1

    if 13 in row:
        thirteen += 1

    if 14 in row:
        fourteen += 1

    if 15 in row:
        fifteen += 1

    if 16 in row:
        sixteen += 1

    if 17 in row:
        seventeen += 1

    if 18 in row:
        eighteen += 1

    if 19 in row:
        nineteen += 1

    if 20 in row:
        twenty += 1

    if 21 in row:
        twentyone += 1

    if 22 in row:
        twentytwo += 1

    if 23 in row:
        twentythree += 1

    if 24 in row:
        twentyfour += 1

    if 25 in row:
        twentyfive += 1

    if 26 in row:
        twentysix += 1

    if 27 in row:
        twentyseven += 1

    if 28 in row:
        twentyeight += 1 

    if 29 in row:
        twentynine += 1

    if 30 in row:
        thirty += 1   

    if 31 in row:
        thirtyone += 1

    if 32 in row:
        thirtytwo += 1

    if 33 in row:
        thirtythree += 1

    if 34 in row:
        thirtyfour += 1

    if 35 in row:
        thirtyfive += 1

    if 36 in row:
        thirtysix += 1

    if 37 in row:
        thirtyseven += 1

    if 38 in row:
        thirtyeight += 1

    if 39 in row:
        thirtynine += 1

    if 40 in row:
        fourty += 1

    if 41 in row:
        fourtyone += 1

    if 42 in row:
        fourtytwo += 1

    if 43 in row:
        fourtythree += 1

    if 44 in row:
        fourtyfour += 1

    if 45 in row:
        fourtyfive += 1

    if 46 in row:
        fourtysix += 1

    if 47 in row:
        fourtyseven += 1

    if 48 in row:
        fourtyeight += 1

    if 49 in row:
        fourtynine += 1

    if 50 in row:
        fifty += 1

    if 51 in row:
        fiftyone += 1

    if 52 in row:
        fiftytwo += 1

    if 53 in row:
        fiftythree += 1

    if 54 in row:
        fiftyfour += 1

    if 55 in row:
        fiftyfive += 1

    if 56 in row:
        fiftysix += 1

    if 57 in row:
        fiftyseven += 1

    if 58 in row:
        fiftyeight += 1

    if 59 in row:
        fiftynine += 1

    if 60 in row:
        sixty += 1

    if 61 in row:
        sixtyone += 1

    if 62 in row:
        sixtytwo += 1

    if 63 in row:
        sixtythree += 1

    if 64 in row:
        sixtyfour += 1

    if 65 in row:
        sixtyfive += 1

    if 66 in row:
        sixtysix += 1

    if 67 in row:
        sixtyseven += 1

    if 68 in row:
        sixtyeight += 1

    if 69 in row:
        sixtynine += 1

GreatestNum = max(one,two,three,four,five,six,seven,eight,nine,ten,
eleven,twelve,thirteen,fourteen,fifteen,sixteen,seventeen,eighteen,nineteen,
twenty,twentyone,twentytwo,twentythree,twentyfour,twentyfive,twentysix,
twentyseven,twentyeight,twentynine,
thirty,thirtyone,thirtytwo,thirtythree,thirtyfour,thirtyfive,
thirtysix,thirtyseven,thirtyeight,thirtynine,
fourty,fourtyone,fourtytwo,fourtythree,fourtyfour,fourtyfive,
fourtysix,fourtyseven,fourtyeight,fourtynine,
fifty,fiftyone,fiftytwo,fiftythree,fiftyfour,fiftyfive,
fiftysix,fiftyseven,fiftyeight,fiftynine,
sixty,sixtyone,sixtytwo,sixtythree,sixtyfour,sixtyfive,
sixtysix,sixtyseven,sixtyeight,sixtynine)
LeastNum = min(one,two,three,four,five,six,seven,eight,nine,ten, 
eleven,twelve,thirteen,fourteen,fifteen, 
sixteen,seventeen,eighteen,nineteen,twenty,twentyone,twentytwo, 
twentythree,twentyfour,twentyfive,
twentysix,twentyseven,twentyeight,twentynine, 
thirty,thirtyone,thirtytwo,thirtythree,thirtyfour,thirtyfive,
thirtysix,thirtyseven,thirtyeight,thirtynine,
fourty,fourtyone,fourtytwo,fourtythree,fourtyfour,fourtyfive,
fourtysix,fourtyseven,fourtyeight,fourtynine,
fifty,fiftyone,fiftytwo,fiftythree,fiftyfour,fiftyfive,
fiftysix,fiftyseven,fiftyeight,fiftynine,
sixty,sixtyone,sixtytwo,sixtythree,sixtyfour,sixtyfive,
sixtysix,sixtyseven,sixtyeight,sixtynine)

print('rows returned:', row_counter)
print('ones found:', one)
print('twos found:', two)
print('threes found:',three)
print('fours found:',four)
print('fives found:',five)
print('sixs found:',six)
print('sevens found:',seven)
print('eights found:',eight)
print('nines found:',nine)
print('tens found:',ten)
print('elevens found:',eleven)
print('twelves found:',twelve)
print('thirteens found:',thirteen)
print('fourteens found:',fourteen)
print('fifteens found:',fifteen)
print('sixteens found:',sixteen)
print('seventeens found:',seventeen)
print('eighteens found:',eighteen)
print('nineteens found:',nineteen)
print('twentys found:',twenty)
print('twentyones found:',twentyone)
print('twentytwos found:',twentytwo)
print('twentythrees found:',twentythree)
print('twentyfours found:',twentyfour)
print('twentyfives found:',twentyfive)
print('twentysixs found:',twentysix)
print('twentysevens found:',twentyseven)
print('twentyeights found:',twentyeight)
print('twentynines found:',twentynine)
print('thirtys found:',thirty)
print('thirtyones found:',thirtyone)
print('thirtytwos found:',thirtytwo)
print('thirtythrees found:',thirtythree)
print('thirtyfours found:',thirtyfour)
print('thirtyfives found:',thirtyfive)
print('thirtysixs found:',thirtysix)
print('thirtysevens found:',thirtyseven)
print('thirtyeights found:',thirtyeight)
print('thirtynines found:',thirtynine)
print('fourtys found:',fourty)
print('fourtyones found:',fourtyone)
print('fourtytwos found:',fourtytwo)
print('fourtythrees found:',fourtythree)
print('fourtyfours found:',fourtyfour)
print('fourtyfives found:',fourtyfive)
print('fourtysixs found:',fourtysix)
print('fourtysevens found:',fourtyseven)
print('fourtyeights found:',fourtyeight)
print('fourtynines found:',fourtynine)
print('fiftys found:',fifty)
print('fiftyones found:',fiftyone)
print('fiftytwos found:',fiftytwo)
print('fiftythrees found:',fiftythree)
print('fiftyfours found:',fiftyfour)
print('fiftyfives found:',fiftyfive)
print('fiftysixs found:',fiftysix)
print('fiftysevens found:',fiftyseven)
print('fiftyeights found:',fiftyeight)
print('fiftynines found:',fiftynine)
print('sixtys found:',sixty)
print('sixtyones found:',sixtyone)
print('sixtytwos found:',sixtytwo)
print('sixtythrees found:',sixtythree)
print('sixtyfours found:',sixtyfour)
print('sixtyfives found:',sixtyfive)
print('sixtysixs found:',sixtysix)
print('sixtysevens found:',sixtyseven)
print('sixtyeights found:',sixtyeight)
print('sixtynines found:',sixtynine)
print('the number of times that the Greatest number has been played 
is',GreatestNum)
print('the number of times that the Least number has been played 
is',LeastNum)
print('Finished!')

1 Ответ

0 голосов
/ 13 октября 2018

Как уже отмечали другие, то, что вы делаете, очень утомительно, и вам следует подумать о том, чтобы пройти еще несколько уроков.Тем не менее, чтобы решить вашу проблему с получением «имени» максимального или минимального числа, вы можете сохранить list строк следующим образом:

number_names = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']
# and so on for all numbers

Тогда вы можете получитьимя числа путем доступа к значению этого индекса в вашем списке.Например:

print(number_names[0]) # prints 'zero'
print(number_names[5]) # prints 'five'
print(number_names[7]) # prints 'seven'

Итак, в ваших последних нескольких утверждениях для печати вы можете иметь:

print('the number of times that the Greatest number has been played is', number_names[GreatestNum])
print('the number of times that the Least number has been played is', number_names[LeastNum])
...