0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python boot camp by Dr.Angela day3

0
Last updated at Posted at 2021-02-16

if/else構文

print("Welcome to the rollercoaster!")
height =int(input("What is your height in cm?"))

if height >= 120:
  print("You can ride the rollercoaster!!")

else:
  print("Sorry, You cannnot ride the rollercoaster...")

:Greater than
<:Less than
=:Greater than or equal to
<=:Less than or equal to
==:Equal to # <--「=」(代入)とは異なるので注意!
!=:Not equal to

Nested if/else/elif 構文

Mission>> BMI program

h =float(input("enter your height in m: "))
w =float(input("enter your weight in kg: "))

bmi =round(w/(h**2))
if bmi < 18.5:
  msg = "underweight"
elif 18.5 <= bmi <25:
  msg = "normal weight"
elif 25 <= bmi < 30:
  msg = "over weight"
elif 30 <= bmi < 35:
  msg ="obese"
else:
  msg = "clinically obese. Go to the hospital immediately"

print(f"BMI:{bmi}\nStatus:{msg}")    # f-stringsでstring型とint型/float型を一気に変換!!

Mission>> Leap year program (うるう年)

みなさんご存じ「うるう年」を西暦から選出するプログラム。
うるう年がどのように表れるのか知らないとコーディングできないので調べました。

「ユリウス暦」で4年に1度のうるう年が制定されましたが、長い長い年月を経ていくと、暦と季節にズレが生じ、やがてそれが無視できないレベルにまで到達します。そこで、ローマ教皇グレゴリウス13世が、地球が太陽をまわる時間を「365日よりわずか0.2422日だけ長い」ことを天体観測で実証し、1582年、現在も使われている「グレゴリオ暦」に改暦されました。
このグレゴリオ暦では、うるう年を次のように定めています。
(1)西暦が4で割り切れる年をうるう年とする
(2)西暦が100で割り切れて、400で割り切れない年は、平年とする
2020年や2024年は(1)に当てはまるので、うるう年となりますが、2100年、2200年、2300年は(2)に当てはまり、うるう年にはなりません。
(出典:https://precious.jp/articles/-/16720)
とのことです。「4年に1度にくるが、誤差がある」程度の認識しかなかったので、勉強になりました(笑)

year =int(input("Which year do you want to check?"))
if year %400 ==0:
  if year %100 ==0 year % 400 !=0:
    print("Normal year")
  else:
    print("Lead year")
else:
  print("Normal year")

このように入れ子(Nest)でif/else文を記述する際、どのifにどのelseが対応していくかわからなくなるため、if文書いたら、ネストする前に同じインデントでelse文を記述すると、ミスを減らせます。また、else:でやることがなければ「pass」を記述するか、「else:」自体書かない。

Mission>> Online AMASON BOOK Order

ECサイト「AMASON BOOK(架空のブランド)」での注文画面

print("Welcome to AMASON BOOK!")
print("What category book do you want?")
size =input("[(P)paperback, (S)softcover, (H)hardcover]: ")
add_bj =input("Do you want to a book jacket?[Y,N]: ")
add_pb =input("Do you want a purchase bag?[Y,N]: ")

total =0
if size =="P":
  total +=5
elif size =="S":
  total +=10
elif size =="H":
  total +=15

if add_bj =="Y":
  total +=2

if add_pb =="Y":
  if size =="P":
    total +=1
  elif size =="S":
    total +=2
  else:
    total +=3
else:
  total -=1
  print("Thanks your ecological activity!!")

print(f"Your final bill is ${total}")

最後の方で、if文のネストがありましたね。
今後、HTMLで動くwebアプリとして使えそう。Flaskあたりで「AMASON BOOK」のサイト作ってみよう。

0
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?