close

201 偶數判斷

說明:

請使用選擇敘述撰寫一程式,讓使用者輸入一個正整數,然後判斷它是否為偶數(even)。

輸入/輸出

image

Code:

x=int(input())
if not(x %2==0 or x==0):
    print(f"{x} is an even number.")
else:
    print(f"{x} is not an even number.")

 

202 倍數判斷

說明:

請使用選擇敘述撰寫一程式,讓使用者輸入一個正整數,然後判斷它是3或5的倍數,顯示【x is amultiple of 3.】或【x is a multiple of 5.】;若此數值同時為3與5的倍數,顯示【x is a multiple of 3 and 5.】;如此數值皆不屬於3或5的倍數,顯示【x is not amultiple of 3 or 5.】,將使用者輸入的數值代入x。

輸入/輸出

範例輸入:

55

範例輸出:

image

Code:

x=int(input())
if x%5==0 or x%3==0:
    if x%5==0 and x%3==0:
        print(f"{x} is multiple of 3 and 5.")
    elif x%5==0:
        print(f"{x} is multiple of 5.")
    elif x%3==0:
        print(f"{x} is multiple of 3.")
else:
    print(f"{x} is not multiple of 3 and 5.")

 

203 閏年判斷

說明:

請使用選擇敘述撰寫一程式,讓使用者輸入一個西元年份,然後判斷它是否為閏年(leap year)或平年。其判斷規則為:每四年一閏,每百年不閏,但每四百年也一閏。

輸入/輸出

範例輸入:

2045

範例輸出:

image

Code:

x=int(input())
if x %4==0:
    if x%100==0:
        if x%400==0:
            print(f"{x} is a leap year")
        else:
            print(f"{x} is not a leap year")
    else:
        print(f"{x} is a leap year")
else:
    print(f"{x} is not a leap year")

 

204 算術運算

說明:

請使用選擇敘述撰寫一程式,讓使用者輸入兩個整數a、b,然後再輸入一算術運算子 (+、-、*、/、//、%) ,輸出經過運算後的結果。

輸入/輸出

範例輸入:

10

25

*

範例輸出:

image

Code:

a=eval(input(""))
b=eval(input(""))
c=input("")
if(c=="+"):
  print(a+b)
elif(c=="-"):
  print(a-b)
elif(c=="*"):
  print(a*b)
elif(c=="/"):
  print(a/b)
elif(c=="%"):
  print(a%b)
elif(c=="//"):
  print(a//b)

 

205 字元判斷

說明:

請使用選擇敘述撰寫一程式,讓使用者輸入一個字元,判斷它是包括大、小寫的英文字母(alphabet)、數字(number)、或者其它字元(symbol)。例如:a為英文字母、9為數字、$為其它字元。

輸入/輸出

範例輸入:

@

範例輸出:

image

Code:

import math
x=input()
if x.isalpha()==True:
    print(f"{x} is a alphabet.")
elif x.isdigit()==True:
    print(f"{x} is a digit.")
else:
    print(f"{x} is a symbol.")

 

206 等級判斷

說明:

請使用選擇敘述撰寫一程式,根據使用者輸入的分數顯示對應的等級。標準如下表所示:

分數 等級
80~100 A
70~79 B
60~69 C
<=59 F

輸入/輸出

範例輸入:

71

範例輸出:

image

Code:

import math
x=int(input())
if x >=80:
    print("A")
elif x>=70:
    print("B")
elif x>=60:
    print("C")
else:
    print("F")

 

207 折扣方案

說明:

請使用選擇敘述撰寫一程式,要求使用者輸入購物金額,購物金額需大於8,000(含)以上,並顯示折扣優惠後的實付金額。購物金額折扣方案如下表所示:

金額 折扣
8000(含) 以上9.5折
18000(含) 以上9折
28000(含) 以上8折
38000(含) 以上7折

輸入/輸出

範例輸入:

25000

範例輸出:

image

Code:

import math
x=eval(input())
if x>=8000:
    if x>18000:
        if x>28000:
            if x > 38000:
                print(x*0.7)
            else:
                print(x*0.8)
        else:
            print(x*0.9)
    else:
        print(x*0.95)

 

208 十進位換算

說明:

請使用選擇敘述撰寫一程式,讓使用者輸入一個十進位整數num(0 ≤ num ≤ 15),將num轉換成十六進位值。

轉換規則 = 十進位0 ~ 9的十六進位值為其本身,十進位10 ~ 15的十六進位值為A~F。

輸入/輸出

範例輸入:

13

範例輸出:

image

Code:

import math
x=eval(input())
if x>=0 and x<=9:
    print(x)
elif x==10:
    print("A")
elif x==11:
    print("B")
elif x==12:
    print("C")
elif x==13:
    print("D")
elif x==14:
    print("E")
elif x==15:
    print("F")

 

209 距離判斷

說明:

請使用選擇敘述撰寫一程式,讓使用者輸入一個點
的平面座標x和y值,判斷此點是否與點(5, 6)的距離
小於或等於15,如距離小於或等於15顯示【Inside】
,反之顯示【Outside】。

計算平面上兩點距離的公式:

輸入/輸出

範例輸入:

7

20

範例輸出:

image

Code:

import math
x=eval(input())
y=eval(input())
dis=((x-5)**2+(y-6)**2)**0.5
if dis<=15:
    print("Inside")
if dis>15:
    print("Outside")

 

210 三角形判斷

說明:

請使用選擇敘述撰寫一程式,讓使用者輸入三個邊長,檢查這三個邊長是否可以組成一個三角形。若可以,則輸出該三角形之周長;否則顯示【Invalid】。

檢查方法 = 任意兩個邊長之總和大於第三邊長。

輸入/輸出

範例輸入:

5

6

13

範例輸出:

image

Code:

x=int(input())
y=int(input())
z=int(input())
if x+y>z and y+z>x and z+x>y:
    print(x+y+z)
else:
    print("Invalid")

arrow
arrow
    文章標籤
    TQC TQC+ python 程式語言
    全站熱搜

    低階ㄇㄋ 發表在 痞客邦 留言(0) 人氣()