iT邦幫忙

2024 iThome 鐵人賽

DAY 11
0
Python

一起來用 Snakify 練練手系列 第 11

【一起來用 Snakify 練練手】Day11 Lesson 4 練習題參考解答-1

  • 分享至 

  • xImage
  •  

Lesson4 單元的題目偏向簡單
主要都是在考你知不知道起始值跟結束值會不會設定
(⭐ 代表加碼題目)

  • ⭐ Count to N
a = int(input())

for i in range (a):
    print(i+1)
  • Series - 1
    ✨ 注意結束值
a = int(input())
b = int(input())

for i in range (a, b+1):
    print(i)
  • ⭐ First N odd, ascending
    ✨ 注意結束值和間隔值
a = int(input())
for i in range (1, a+1, 2):
    print(i)
  • Series - 2
a = int(input())
b = int(input())

if a>b:
    for i in range (a, b-1, -1):
        print(i)
else:
    for i in range (a, b+1, 1):
        print(i)
  • ⭐ First N even, descending
    ✨ 注意,這裡要遞減喔
a = int(input())
for i in range (a, -1, -1):
    if i%2==0:
        print(i)
  • Sum of ten numbers
sum = 0
for i in range(10):
    sum += int(input())
print(sum)
  • Sum of N numbers
a = int(input())
sum = 0
for i in range(a):
    sum += int(input())
print(sum)
  • ⭐ Product of N numbers
a = int(input())
sum = 1
for i in range(a):
    sum *= int(input())
print(sum)
  • Sum of cubes
a = int(input())
sum = 0
for i in range (0, a+1, 1):
    sum += i**3
print(sum)
  • Factorial
a = int(input())
ans = 1

for i in range (1, a+1):
    ans *= i

print(ans)
  • The number of zeros
a = int(input())
sum = 0
for i in range (a):
    if int(input()) == 0:
        sum += 1

print(sum)
  • Adding factorials
    ✨ 這裡可能需要兩層迴圈
a = int(input())
sum = 0
for i in range (1, a+1):
    temp = 1
    for j in range (1, i+1):
        temp *= j
    sum += temp
print(sum)
  • ⭐ Squares in range
a = int(input())
b = int(input())
for i in range (a, b+1):
    print('{}*{}={}'.format(i, i, i*i))
  • Ladder
a = int(input())
for i in range(1, a+1):
    for j in range (1, i+1):
        print(j, sep='', end="")
    print("")
  • ⭐ Is prime
n = int(input())
answer = 'PRIME'
for i in range(2, n - 1):
    if n % i == 0:
        answer = 'COMPOSITE'
print(answer)
  • ⭐ Print primes in range
    ✨ 可以先寫下來,找關係
a = int(input())
b = int(input())

for i in range(a, b+1):
    for j in range (2, i):
        if (i%j)==0:
            break
    else:
        print(i)
  • ⭐ Number of primes in range
    ✨ 如果上一題做得出來,這一題應該沒問題
a = int(input())
b = int(input())
sum = 0

for i in range(a, b+1):
    for j in range (2, i):
        if (i%j)==0:
            break
    else:
        sum += 1
print(sum)
  • Lost card
    ✨ 梯行公式:(上底➕下底)✖️高➗2
a = int(input())
sum = 0
total = int((1+a)*a/2)

for i in range (1, a):
    sum += int(input())

print(total-sum)

上一篇
【一起來用 Snakify 練練手】Day10 Lesson 4 概念
下一篇
【一起來用 Snakify 練練手】Day12 Lesson 5 概念
系列文
一起來用 Snakify 練練手30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言