Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Simple if/else expressions (1/2) | Conditional statements
/
Learn Python from Scratch
セクション 4.  3
single

single

bookSimple if/else expressions (1/2)

メニューを表示するにはスワイプしてください

Okay, now we are familiar with conditional operators and know how to deal with them. It means now we can divide our code into pieces depend on the condition. In Python, its implemented like the follows:

1234
if condition: do if true else do if not true
copy

For example, remember our primitive example about deciding whether to go walk or not. We can write it in Python syntax like that:

1234
if (weather is good and (day is Sunday or day is Saturday)): go walk else: stay home :(
copy

Consider some examples: imagine you have some string and want to check if it's wide. Let's call string wide if it has more than 20 symbols.

1234567891011121314151617
# assign some variable test = "small string" # conditional statement if(len(test) > 20): print("This string is wide!") else: print("Nothing special") # check on different string test = "This string is very-very and very large" # conditional statement if(len(test) > 20): print("This string is wide!") else: print("Nothing special")
copy

Please note, that code blocks written below if, else statements need to be indented on the same level (2 spaces, 3, 4, ...). The main condition - the equal number.

Assume you own a small grocery and spend 2000 dollars every day. Obviously, if your revenue per day is less than 2000, then you suffer losses, otherwise - nothing bad. Let's try to write if/else statement for that.

タスク

スワイプしてコーディングを開始

Write conditional statement checking if variable revenue is less than 2000 (in that case print message "We suffer losses!", otherwise - print "Everything is ok!"). Do not change value of revenue.

解答

Switch to desktop実践的な練習のためにデスクトップに切り替える下記のオプションのいずれかを利用して、現在の場所から続行する
すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 4.  3
single

single

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

some-alt