Add keyboard interrupt

This commit is contained in:
jimmy 2022-09-17 21:07:35 +12:00
parent f09d4ba213
commit 20d5499bf9
1 changed files with 29 additions and 26 deletions

View File

@ -4,30 +4,33 @@ random.seed()
number = random.randrange(100, 999) number = random.randrange(100, 999)
number = list(str(number)) number = list(str(number))
guesses = 0 guesses = 0
while True: try:
while True:
if guesses == 5:
print("You loose!") if guesses == 5:
for value in number: print("You loose!")
print(value, end=" ") for value in number:
break print(value, end=" ")
hint = [" ", " ", " "] break
guess = input("Enter a three digit number:") hint = [" ", " ", " "]
while len(guess) != 3:
guess = input("Enter a three digit number:") guess = input("Enter a three digit number:")
while len(guess) != 3:
guess = list(guess) guess = input("Enter a three digit number:")
if guess == number:
print("You win!") guess = list(guess)
break if guess == number:
for index, value in enumerate(guess): print("You win!")
print(value, end=" ") break
if value in number: for index, value in enumerate(guess):
hint[index] = "P" print(value, end=" ")
print("") if value in number:
for index, value in enumerate(guess): hint[index] = "P"
if number[index] == value: print("")
hint[index] = "F" for index, value in enumerate(guess):
print(hint[index], end=" ") if number[index] == value:
guesses += 1 hint[index] = "F"
print("\n") print(hint[index], end=" ")
guesses += 1
print("\n")
except KeyboardInterrupt:
pass