我创建了一个Mastermind代码,当我尝试在Mac上运行它时,它只是说“注销”。如果有人知道为什么,那将是非常有帮助的!这是代码:
def masterMind():
number = random.ranint(10000,99999) #the computer chooses 5 random numbers
userGuess = raw_input("Guess my 5 digit password:") #asking the user to input their guess
tries = 10 # telling the computer number of tries the user is allowed
while tries < 0: # 10 attempts is the maximum amount
if number != userGuess: # if the computer's password does not equal the user's guess then it equals to one attempt
tries += 1
userGuess = input("Guess my 5 digit password:")
else: #if the user and the computer's answers align
print "Win: ", userGuess
print number
答案 0 :(得分:2)
tries = 10
while tries < 0:
将从不进入循环。
您可能希望使用>
来反转比较感。
你还需要在循环中递减 tries
而不是递增它。
答案 1 :(得分:0)
更多“pythonic”(2.x)方式。修复了“00000”之类的答案,将int
修复为str
对等。
def masterMind():
number = "%05d" % random.randint(0, 99999) #the computer chooses 5 random numbers
for tries in range(10):
user_guess = raw_input("Guess my 5 digit password:") #asking the user to input their guess
if number == user_guess:
print "Win on the %d try" % (tries + 1)
break
print "answer was:", number