2014-11-13 9 views
0

사용자가 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 "" 
+2

오, 이런. 함수를 사용하는 법을 배워야합니다! 이를 작은 블록으로 분해하면 훨씬 더 읽기 쉽고 쉽게 수정할 수 있습니다. –

답변

-1
import random 
count=0 
user=raw_input("Welcome to Guess the Number! Please enter a number from 1-100: ") 

go = False 

while(go is True): 
    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!" 
     user1=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": 
      user1=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 "" 
       #Change it so that you change go to False 
       #The loop will not execute again 
       go = False 

당신이 밖으로 깨진 때까지 실행 루프의 어떤 종류에 포장 것입니다 (나는 그것을 실행되지 않은) 작동합니다. 이 경우에는 go라는 부울을 테스트 한 while 루프를 사용했습니다. 원래 사실인데, while 루프는 반복적으로 반복 될 것입니다. 그러나 사용자가 멈추고 자 할 때 나는 False로 설정하여 처리합니다. while 루프 이후에 실행할 다른 것이 없으므로 go가 이제 false이고 프로그램이 끝나기 때문에 while 루프가 실행되지 않습니다.

+0

정말 고마워요! –

2
import random 

def play_game(): 

    # Much nicer than int(float(random.randrange(0,101))) 
    computer = random.randint(0, 101) 
    count = 0 

    # Keep looping until we return 
    while True: 

     count += 1 
     user = int(raw_input('Please guess a number: ')) 

     if user < computer: 
      print 'too low!' 
     elif user > computer: 
      print 'too high!' 
     else: 
      print 'You win!' 
      print 'It took you {} tries to get the right answer!'.format(count) 
      return # Leave play_game 

def main(): 

    print 'Welcome!' 

    while True:  
     play_game() 

     play_again = raw_input('Play again? y/n: ') == 'y' 
     if not play_again: 
      return # Leave main 

main()