# 5.AB猜數字遊戲(PythonDemo5) ###### tags: `PythonDemo` **分類:字元與字串** **說明** 輸入四位不重覆數字 (0~9), 做為電腦的題目 輸入四位不重覆數字 (0~9), 做為您猜的答案 不需檢查使用者輸入之格式 當輸入之答案與題目相同, 程式結束(輸出:You Win!) 遊戲規則 電腦的題目: 1234 您猜的數字: 5283 結果為 1A1B 表示您共猜對了兩位數,其中有一個字位置對,另一個字位置不對 其中,A 表示猜對一個字且位置也對,B 表示猜對一個字但是位置不對。 **Input Format** 四位數字的答案 四位數字猜的數字..... 四位數字猜的數字..... 四位數字猜的數字..... **Output Format** 幾A幾B --- ```python= import random count = 0 digit = ('0123456789') ans = input() word = '' while 1: count += 1 a, b = 0, 0 # 0A0B word = input() for i in range(4): if word[i] == ans[i]: a += 1 elif word[i] in ans: b += 1 print ("%sA%sB" % (a, b)) if word == ans: print ('You Win!') break ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up