程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Write 1a2b games in Python

編輯:Python

requirement :

1A2B Guess the number of trips Play 1. swim The rules of the play 1. Random Generate a four digit number ( The first can be 0, Every time Digit number inequality ), Hereinafter referred to as answer case 2. use Give to Make a guess measuring Four digits of 3. When used Household guess measuring The number of ( Four place ) Exist in answer , but Incorrect position indeed when , Remember as A 4. When used Household guess measuring The number of ( Four place ) Exist in answer , And The position is positive indeed when , Remember as B 5. Program return A Of remember Sum of numbers B Of remember Count , Format by : XAXB 2. Example 3. demand Yes To use Household It's illegal transport It has certain resistance ( Check digit , Are they all numbers , Whether to include empty grid ) When used Households lose Enter into save when , Save the current tour Play shape state ( Generate... Locally Record file , recommend json Format ), use Household can choice Exit the tour Play or continue swim Play When used Households lose Enter into exit when , Exit the tour Play Every time Bureau tour Dramatize beginning when , Check Is there a local saved tour Play , If there is , use Household can Choose to continue swim Play or open Start a new tour Play   Code :
# -*- codeing = utf-8 -*-
# @Time : 2022/1/16 11:35
# @Author : B20041409
# @File : 1A2BFUB.py
# @Software : PyCharm
import random
import os
if os.path.exists("D:\\new.txt")==False: # Check whether the file exists
file = open('D:\\' + 'new' + '.txt','w') # If it does not exist, it will be created immediately
file.close()
temp=1 # Controls whether to continue
for i in range(1000):
a = int(random.randint(1000, 9999))
aa = [int(a / 1000), int((a / 100) % 10), int((a / 10) % 10), int(a % 10)]
if any([aa[0] == aa[1], aa[0] == aa[2], aa[0] == aa[3], aa[1] == aa[2], aa[1] == aa[3], aa[2] == aa[3]]):
pass
else:
break
print(" Has randomly generated a different 4 Digit number ")
while temp ==1 :
file = open('D:\ Manna \python\pythonProject1\ new.txt', 'w')
b = input(" Please enter a different 4 Digit number , Guess the number :")
if b.isdigit():
b = int(b)
if b<9999 and b>999:
bb = [int(b / 1000), int((b / 100) % 10), int((b / 10) % 10), int(b % 10)]
(i, j) = (0, 0)
for y in range(0, 4):
if aa[y] == bb[y]:
i += 1
else:
if any([aa[y] == bb[0], aa[y] == bb[1], aa[y] == bb[2], aa[y] == bb[3]]):
j += 1
print("%dA%dB" % (i, j))
file.write("'%d' The result is '%dA%dB'"%(b,i,j))
string=input(" Please input if you want to continue y:")
file.close()
if string !="y":
temp=0
continue
 This code , Enter some data , Will judge whether it meets the requirements , If you do not meet the requirements, you will be asked to re-enter , After each time, there will be a question whether to continue , write in y You can do it , Writing other characters will exit 

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved