이번엔 파이썬으로 컴퓨터가 생각한 숫자 맞추기 게임 코딩을 따라해보다.

[컴퓨터가 생각한 숫자 맞추기 Python 코드]

import random def guessNum(): correctNum = random.randint(1,100) guess = 0 tries = 0 min = 1 max = 100 while guess != correctNum and tries < 6: print("Guess my thinking number(",min,"~",max,"): ") print("now you got",(6-tries),"chances left") tempNum = input() guess = int(tempNum) print("Your guess is", guess, "...") if guess < correctNum: print("my number is Bigger than", guess) min = guess + 1 elif guess > correctNum: print("my number is Smaller than", guess) max = guess - 1 tries += 1 if guess == correctNum: print("Yes!",correctNum,"That's my thinking number!") print("you find my number in", tries) newGame() else: print("No, You are out of chances.") print("my thinking number is", correctNum) newGame() def newGame(): sel = input("Do you want to play another game?(y/n)") if sel == "y": print("-----------------------------------") guessNum() else: print("then play another time. bye~") guessNum()

역시 파이썬은 심플하고 직관적이다.

한데 코드를 올릴려고 보니 HTML에서는 기본적으로 띄어쓰기 조절이 안돼서 파이썬처럼 블록구조가 생명인 언어의 경우 html 상으로 옮길 때 문제가 발생했었는데 <pre></pre> 코드 사이에 넣으면 위 처럼 깔끔하게 띄어쓰기 문제가 해결되는걸 알게되다. 위 코드의 경우 <pre></pre> 코드 사이에 넣고 폰트 사이즈를 3으로 한 것.