一个播放游戏变体的程序 Mastermind与用户。例如,程序应该生成一个4位数字 随机数。然后应该允许用户猜测直到用户 得到正确的数字。应该向用户提供线索,说明有多少 猜测的数字是正确的,在正确的位置,有多少是正确的,但在错误的地方。?
答案 0 :(得分:0)
import random
number=random.randint(1000,9999)
number=str(number)
answer='incorrect'
knowns=['?','?','?','?']
while answer=='incorrect':
print(''.join(knowns))
guess=input("guess the number:")
if guess!=number:
print("you guessed incorrectly")
if number[0]==guess[0]:
knowns[0]=number[0]
print("You got the 1st digit correct")
if number[1]==guess[1]:
knowns[1]=number[1]
print("You got the 2nd digit correct")
if number[2]==guess[2]:
knowns[2]=number[2]
print("You got the 3rd digit correct")
if number[3]==guess[3]:
knowns[3]=number[3]
print("You got the 4th digit correct")
print("")
if guess==number:
print("you guessed correctly")
answer='correct'
#could just use 'break'
它不是特别有效,但应该适合您的目的。