2桁の正の整数を2個乱数を用いて発生させ,その大きい方から小さい方まで順に表示するプログラムを作成しましょう.
Create a program that generates two two-digit positive integers using random numbers and displays them in order from largest to smallest.
|
Student number: s246099 Numbers from 58 to 14 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 ------------------------ |
教科書 p.104 の range 関数のところを参考にしてください.なお,結果の出力では必ず最初の行に自分の学生番号を,最後にハイフンによるラインをつけること.無い場合には減点するので注意.これらは解答用紙にあらかじめ入っているものを自分のものに修正するだけでよいので,必ず行ってください.
Refer to the section on the range function on page 104 of the textbook.
|
解答用紙を使用する際には,学生番号と名前の記入も忘れないでください.さらに,解答用紙自体がPythonのプログラムとなっていますので,実行してエラーの無いことを確認してから提出してください. 指定の解答用紙を使用していない,実行時にエラーが出る,学生番号と名前が無い,というような答案は提出されても採点しません.注意してください. |
解答例
# #############################
#
# プログラミング入門I 確認テスト 2025.12.1
# 学生番号: s246099
# 氏名: 松江 花子
#
# #############################
import random
print('Student number: s246099')
print()
large = random.randint(10, 99)
small = random.randint(10, 99)
small, large = list(sorted([small, large]))
print(f'Numbers from {large} to {small}')
for i in range(large, small - 1, -1):
print(i, end = ' ')
else:
print()
print('\n------------------------\n')
|