马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
闲的无聊做了一个以前玩过的猜数游戏 几A几B的那个 应该有人玩过吧。新手 刚学python 非常菜 做的代码感觉非常笨 求大家帮忙改进下。:ton: import random
print('--------------------------欢迎来到猜数游戏--------------------------')
print('作者:二峰')
print('''这是一个猜数游戏,我这里有随机的四个数(不重复),你来猜,位置和数都对了\n算A,只有数对了算B,不允许输入重复的数字,数字范围是0至9.来试一试吧.''')
print('''(输入'restart'重新开始,输入'quit'退出,输入'answer'显示答案)''')
def game():
A = 0
B = 0
C = 0
zero = random.randint(0,9)
one = random.randint(0,9)
while one == zero:
one = random.randint(0,9)
two = random.randint(0,9)
while two==zero or two==one:
two = random.randint(0,9)
three = random.randint(0,9)
while three==zero or three==one or three==two:
three = random.randint(0,9)
num = input('请输入4个数字:')
if num != 'quit' and num != 'answer' and num != 'restart':
while num != str(zero) + str(one) + str(two) + str(three):
if num == 'quit' or num == 'answer' or num == 'restart':
break
while len(num)!=4 or num[0]==num[1] or num[0]==num[2] or num[0]==num[3] or num[1]==num[2] or\
num[1]==num[3] or num[2]==num[3] or not num.isdigit():
print('您输入的格式不对,请重新输入.')
num= input('请输入4个数字:')
if num == 'quit' or num == 'answer' or num =='restart':
break
if num == 'quit' or num == 'answer' or num =='restart':
break
if num == str(zero) + str(one) + str(two) + str(three):
break
if num[0] == str(zero):
A = A+1
elif num[0]==str(one) or num[0]==str(two) or num[0]==str(three):
B = B+1
if num[1] == str(one):
A = A+1
elif num[1]==str(zero) or num[1]==str(two) or num[1]==str(three):
B = B+1
if num[2] == str(two):
A = A+1
elif num[2]==str(zero) or num[2]==str(one) or num[2]==str(three):
B = B+1
if num[3] == str(three):
A = A+1
elif num[3]==str(zero) or num[3]==str(one) or num[3]==str(two):
B = B+1
C = C+1
print(str(A) + 'A' + str(B) + 'B')
A = 0
B = 0
num= input('请输入4个数字:')
if num == str(zero) + str(one) + str(two) + str(three):
print('4A0B')
C = C+1
print('你用了' + str(C) +'次!')
num = input("是否要继续游戏:(输入'quit'退出,输入'restart'重新开始):")
while num != 'quit' and num != 'restart':
print('您输入的格式不对,请重新输入.')
num = input("是否要继续游戏(输入'quit'退出,输入'restart'重新开始):")
if num == 'answer':
print('答案是:' + str(zero) + str(one) + str(two) + str(three))
num = input("是否要继续游戏(输入'quit'退出,输入'restart'重新开始):")
while num != 'quit' and num != 'restart':
print('您输入的格式不对,请重新输入.')
num = input("是否要继续游戏(输入'quit'退出,输入'restart'重新开始):")
if num == 'quit':
print('Bye bye!')
if num == 'restart':
print('----------游戏已重新开始----------')
game()
game()
|