사용자가 1에서 100 사이의 숫자를 입력하도록 요청하는 프로그램을 만들면 프로그램에서 사용자가이 숫자가 너무 높거나 낮을 때 및 성공했을 때이를 알립니다. 그들이 이길 때, 그들은 다시 놀 것인지 아니면 멈추고 싶은지 묻게됩니다. 문제는 프로그램을 재생하여 게임을 재생하는 방법을 모르겠다는 것입니다. 도움말은 크게 감사하겠습니다 (그리고 대부분의 사람들이 def를 사용하고 싶어한다는 것을 알고 있지만 사용법을 모르므로 사용하지 않으시면 감사하겠습니다) 감사합니다. 이 코드를 가정게임을 어떻게 재생합니까?
는import random
count=0
user=raw_input("Welcome to Guess the Number! Please enter a number from 1-100: ")
user=int(float(user))
computer=random.randrange(0,101)
computer=int(float(computer))
while user!=computer:
if user<computer:
user=raw_input("This number is too low! Please try again: ")
user=int(float(user))
count+=1
if user>computer:
user=raw_input("This number is too high! Please try again: ")
user=int(float(user))
count+=1
else:
count+=1
print "You win! The computer entered: " + str(computer) + " It took you " + str(count) + " tries to get the right answer!"
user=raw_input("If you would like to play again, please enter 'play' and if you would like to stop, please enter 'stop': ")
while user!="play" and user1!="stop":
user=raw_input("Thats not what I asked for! If you would like to play again, please enter 'play' and if you would like to stop, please enter 'stop': ")
if user=="play":
count=0
computer=random.randrange(0,101)
computer=int(float(computer))
while user!=computer:
if user<computer:
user=raw_input("This number is too low! Please try again: ")
user=int(float(user))
count+=1
if user>computer:
user=raw_input("This number is too high! Please try again: ")
user=int(float(user))
count+=1
else:
count+=1
print "You win! The computer entered: " + str(computer) + " It took you " + str(count) + " to get the right answer!"
user=raw_input("If you would like to play again, please enter 'play' and if you would like to stop, please enter 'stop': ")
if user=="stop":
print ""
오, 이런. 함수를 사용하는 법을 배워야합니다! 이를 작은 블록으로 분해하면 훨씬 더 읽기 쉽고 쉽게 수정할 수 있습니다. –