TQC+ 程式語言Python 409 得票數計算
第4類:進階控制流程

TQC+ 程式語言Python 409 得票數計算

題目說明:

某次選舉有兩位候選人,分別是No.1: Nami、No.2: Chopper。請撰寫一程式,輸入五張選票,輸入值如為1即表示針對1號候選人投票;輸入值如為2即表示針對2號候選人投票,如輸入其他值則視為廢票。每次投完後需印出目前每位候選人的得票數,最後印出最高票者為當選人;如最終計算有相同的最高票數者或無法選出最高票者,顯示【=> No one won the election.】。

輸入與輸出會交雜如下,輸出的部份以粗體字表示


2
Total votes of No.1: Nami =  0
Total votes of No.2: Chopper =  1
Total null votes =  0
1
Total votes of No.1: Nami =  1
Total votes of No.2: Chopper =  1
Total null votes =  0
8
Total votes of No.1: Nami =  1
Total votes of No.2: Chopper =  1
Total null votes =  1
2
Total votes of No.1: Nami =  1
Total votes of No.2: Chopper =  2
Total null votes =  1
2
Total votes of No.1: Nami =  1
Total votes of No.2: Chopper =  3
Total null votes =  1
=> No.2 Chopper won the election.
 

程式碼:

Nami = Chopper = null = 0

for i in range(5):
    n = eval(input())
    if n == 1:
        Nami += 1
    elif n == 2:
        Chopper += 1
    else:
        null += 1
    
    print('Total votes of No.1: Nami =  %d' %Nami) # 等於後面要有兩個空格。
    print('Total votes of No.2: Chopper =  %d' %Chopper) # 等於後面要有兩個空格。
    print('Total null votes =  %d' %null) # 等於後面要有兩個空格。

if Nami > Chopper:
    print('=> No.1 Nami won the election.')
elif Chopper > Nami:
    print('=> No.2 Chopper won the election.')
else:
    print('=> No one won the election.')

 

TQC+ 程式語言Python 第4類:進階控制流程





如果覺得文章內容還不錯的話,麻煩請幫我點個讚!感謝

可以多點幾次喔~~

第一次點讚需使用 Google 或 Facebook 帳號註冊

一個評論

發表迴響