LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

Python練習問題 回答編

Last updated at Posted at 2019-11-19

練習問題1回答

import random
my_no = random.randint(0, 100)

if my_no >= 80:
    print(str(my_no) + " " + str(my_no*2))
elif my_no < 50:
    print(str(my_no) + " " +  str(my_no/2))
else:
    print(str(my_no) + " " +  str(0))

解説

  1. import randomで利用するライブラリを使えるようにする。
  2. my_noという変数(変数名はなんでも良い)に random.randint(0,100)で得た数値を代入する
  3. if文で条件を記載する
  4. 空白を間に挟むため、int型の数値を str に変換してあげる
0

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