Python

Python作品07-猜數字1A2B

Python作品07-猜數字1A2B

猜數字1A2B

•玩法說明: 1.一般來說,遊戲是一個人設定沒有重複的0~9數字的4位數,由另一方來猜。

2.猜方每猜一個數,數字一定得4位數,數字也不可重複。

3.設定方就給出幾A幾B,猜中的數字,如果位置也對,則累計一個A;位置不對,則累計一個B;如果都沒有猜中數字,就是0A0B

4.重複步驟2~3,直至猜中4A0B為止。

image

# -*- coding: utf-8 -*-
"""
Created on Fri Nov 20 15:21:46 2020

@author: shou
"""

import random

pwd = random.sample(range(0,10),4)
#print(pwd)

A = 0
B = 0

while A != 4:
    num =input("請輸入四個數字:")
    while len(num)!= 4 or len(set(num)) != 4:
        num = input("請輸入四個數字,且數字不能重複:")
    list1 = list(map(int,list(num))  )
    #print(list1)
    A = 0
    B = 0
    for i in list1:
        if i in pwd:
            if list1.index( i ) == pwd.index( i ):
                A += 1
            else:
                B += 1
    print(num,":",A,"A",B,"B",sep="")

print("你猜對了密碼是",num)
comments powered by Disqus
| copyright © 2020 Themefisher All Rights Reserved |