"if"를 "elif"로 변경하려고하면 오류가 발생합니다. 이 코드는 if를 사용할 때 완벽하게 작동하지만 대신 "elif"를 사용하려고하면 구문 오류가 발생합니다. if 문 중 하나만 실행하기를 원하기 때문에 "elif"를 사용해야합니다.elif에서 구문 오류가 발생하지만
guess_row=0
guess_col=0
ship_location=0
number_of_attempts=3
guess_location = input("Guess :").split(",")
guess_row,guess_col = int(guess_location[0]),int(guess_location[1])
if guess_row not in range(1,6):
print("Out of range1.")
print(guess_location)
print(ship_location)
if guess_col not in range(1,6):
print("Out of range2.")
print(guess_location)
print(ship_location)
if ship_location == guess_location:
print("You sunk my battleship! You win!")
else:
print ("You missed!")
print ("You have " + str(number_of_attempts-1) + " attempt(s) left!")
print ("Try again!")
number_of_attempts-=1
을하지만 2 또는 3 "의 경우"을 "의 elif"변경하는 경우 :이 코드는 잘 작동
guess_row=0
guess_col=0
ship_location=0
number_of_attempts=3
guess_location = input("Guess :").split(",")
guess_row,guess_col = int(guess_location[0]),int(guess_location[1])
if guess_row not in range(1,6):
print("Out of range1.")
print(guess_location)
print(ship_location)
elif guess_col not in range(1,6):
print("Out of range2.")
print(guess_location)
print(ship_location)
elif ship_location == guess_location:
print("You sunk my battleship! You win!")
else:
print ("You missed!")
print ("You have " + str(number_of_attempts-1) + " attempt(s) left!")
print ("Try again!")
number_of_attempts-=1
을 나는 구문 오류가 발생합니다. 도움?
당신은'if/elif/else' 스코프를 끝낸 print 문을 사용합니다. – MikeTheLiar