B
B
barakuda12021-12-14 03:41:51
Python
barakuda1, 2021-12-14 03:41:51

Not writing or outputting array incorrectly in python 3.9.1?

Python 3.9.1

import datetime

now = datetime.datetime.now()
iGetDay = []

def dow(date):
    days=["понедельник","вторник","среда","четверг","пятница","суббота","воскресенье"]
    iGetDay = date.weekday()

def hour_func():
  iCurrentHour = now.hour #Получаю текущий час

  iResult = 0

  if iCurrentHour == 10:
    print('Все верно, сейчас 10:00')
    iResult = 1
  elif iCurrentHour == 5:
    print('Все верно, сейчас 05:00')
    iResult = 2
  else:
    print('Сейчас не 10:00 и не 05:00')
    iResult = 3


  print('Текущий час: %d' % iCurrentHour)

  return iResult

def day_func():
  iCurrentDay = iGetDay #Получаю текущий день

  iResultDay = 0

  if iCurrentDay == 'понедельник':
    print('Все верно, сейчас понедельник')
    iResultDay = 1
  elif iCurrentDay == 'суббота':
    print('Все верно, сейчас суббота')
    iResultDay = 2
  elif iCurrentDay == 'воскресенье':
    print('Все верно, сейчас воскресенье')
    iResultDay = 2
  else:
    print('Сейчас не вторник и не суббота')
    iResultDay = 3

  print('Текущий день: %s' % iGetDay)
  print(iGetDay)

  return iResultDay

def main():
    iDay = day_func()
    iHour = hour_func()
  
    print('Main')
    print('день: %s' % iDay)
    print('час:%d' % iHour)
    print('----')

    
    if iDay == 2: #iResult = 2? Проверка на выходные дни
        print('Сегодня выходные')
    else:
        print('Сегодня будни')

    if iHour == 4: # Сейчас 10:00?
        print('Сейчас утро, 10:00')
    else:
        print('Какое-то другое время')


main()


Outputs an empty array or outputs incorrectly:

print('Текущий день: %s' % iGetDay)
  print(iGetDay)


Overall result:

Сейчас не вторник и не суббота
Текущий день: []
[]
Сейчас не 10:00 и не 05:00
Текущий час: 1
Main
день: 3
час:3
----
Сегодня будни
Какое-то другое врем

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alan Gibizov, 2021-12-14
@phaggi

Read about scopes (namespaces).
You have two iGetDay lists. One is in the global namespace of the module, the other is in the local space of the dow function.
These are different objects that are not related to each other in any way.
You'd be better off returning a value from a function with return and linking the result of the function to a global name outside the function.
And all values ​​should be explicitly passed to the function as function parameters and received from the function as return. And don't rely on scopes, they're implicit and easy to get confused about.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question