你正在玩战列舰,但你一直忘记你已经选择过的格子中的哪个方格!编写程序以帮助您跟踪您遇到的方块。您的程序应以坐标读取,每行一个。如果您之前没有尝试过那个方格,那么您的程序应该打印出Hit。如果你已经尝试过那些坐标,你应该打印出你已经选择了这个方格。 您的程序应该运行,直到输入一个空行,并且应该像这样工作:
import re
name = input()
if re.match('[a-zA-Z]+', name): # returns None if not match, a match object otherwise.
print("Thank you for entering your name, {}.".format(name))
else:
print("Oops! You didn't entered a valid name.")
答案 0 :(得分:0)
这是执行所需任务的功能。请记住,没有与此一起出现的错误检查,并且实际上并未检查敌人所在的某些区块的输入。
def battleship():
guesses = []
while True:
guess = str(input("Guess: "))
if guess in guesses:
print("You've chosen that square already")
else:
print("Hit {0}".format(guess))
guesses.append(guess)
示例输出:
>>> battleship()
Guess: A3
Hit A3
Guess: C4
Hit C4
Guess: A3
You've chosen that square already
Guess: B2
Hit B2
Guess:
答案 1 :(得分:0)
这可能会帮助您完成作业:
Hit_Square = []
user_input = ()
while user_input != "":
user_input = input("Guess: ")
if user_input == "":
break
if user_input not in Hit_Square:
Hit_Square.append(user_input)
print ("Hit", user_input)
else:
print("You've chosen that square already")