BaşlayınÜcretsiz Başlayın

Assigning variables with Boolean operators

You can use the fact that and and or are short-circuit operators to assign objects to variables in intelligent ways. Pretend that you are deciding what actions you should take with a client's account based on what they entered in a web-form. Unfortunately, they could submit the form with this field empty, so you need to set a default action just in case. The supplied variable input_action has the contents submitted by the client. The supplied variable is_trading_day is True if today is a day that trades are possible.

Bu egzersiz

Intermediate Python for Finance

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • If your client entered an action, assign it to the variable action. If they entered nothing, use the default action "Hold".
  • Assign the action to the variable do_action if trades can be done today, otherwise assign it as False.
  • Print the action that should be done.

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

# Assign a default action if no input
action = input_action ____ "Hold"

# Print the action
print(action)

# Assign action only if trades can be made
do_action = is_trading_day ____ action

# Print the action to do
____(____)
Kodu Düzenle ve Çalıştır