2011-09-01 3 views
1

나는 단지 파이썬을 배우며이 글을 썼지 만 모든 추측과 너무 높거나 낮을지를 보여주고 싶다. "responseList"부분은 도움이 필요한 부분입니다. 감사!추측의 목록 파이썬

import random, easygui 

    secret = random.randint (1, 100) 
    guess = 0 
    tries = 0 

    easygui.msgbox ("""Guess the secret number. 
    It is from 1 to 99. You have five tries. Get Guessin' !""") 

    while guess != secret and tries < 5: 
     user_response = guess = easygui.integerbox ("C'mon...GUESS!!! ") 

     if not guess: break 
     if guess <= (secret + 5) and guess > secret: 
      easygui.msgbox(str(guess) + " is too HIGH... but you're close!") 
     if guess >= (secret - 5) and guess < secret: 
      easygui.msgbox(str(guess) + " is too LOW... but you're close!")   
     if guess < (secret - 5): 
      easygui.msgbox(str(guess) + " is too LOW... Guess higher")   
     if guess > (secret + 5): 
      easygui.msgbox (str(guess) + " is too HIGH...Guess lower") 

     tries = tries + 1 

     responseList = [user_response] 
     easygui.msgbox (responseList) 

    if guess == secret: 
     easygui.msgbox ("Darn! You got it!") 

    else: 
     easygui.msgbox ("Ha, Ha, Ha! No more guesses! To the firin' squad with ya!") 
     easygui.msgbox (str(secret) + " was the secret number") 
+0

글쎄,'responseList'가 정확히 포함하고 싶은 것은 무엇입니까? –

답변

3

나는 모든 사용자의 응답 목록을 responseList에 포함 시키길 원합니다. 너는 그것을 쓰지 않았다. :)

시작시 빈 목록에 responseList을 설정하고 새로 응답 할 때마다 append으로 설정해야합니다.

responseList = [user_response] 매번 한 요소 목록 만 설정하면됩니다. 분명히 마지막 응답 만있는 단일 요소 목록으로 끝납니다.

1

while guess != secret and tries < 5: 루프 앞에 먼저 responseList을 초기화하십시오. 루프에서 또는 'LOW' 값을 저장하려면 변수를 사용하십시오 (예 : where). 그런 다음 while 루프 외부 easygui.msgbox으로, 포맷 된 결과를 표시 : responseString와 라인

responseList = [] 
while guess...: 
    user_response = ... 
    if not... 
    if guess <=... 
     where = 'HIGH' 
    if guess >=... 
     where = 'LOW' 
    if guess <... 
     where = 'LOW' 
    if guess >... 
     where = 'HIGH' 


    tries... 
    responseList.append((guess, where)) 

responseString = ', '.join([ '%d (%s)' % (guess, where) 
          for guess, where in responseList]) 
easygui.msgbox(responseString) 

List Comprehension, 당신은 위로 읽거나 여기에 대해 요청할 수있다.

1

EasyGUI는 표준 Python 배포판에 포함되어 있지 않습니다. 소스 코드는 http://easygui.sourceforge.net/에서 다운로드 할 수 있습니다. 첫 번째 시도에서 "setup.py install"만 사용하여 Python (x, y) 설치에 설치되었습니다. 예상대로 행동하는 목록을 얻으려면,이 버전을 시도해보십시오

import random, easygui 

secret = random.randint (1, 100) 
guess = 0 
tries = 0 

easygui.msgbox ("""Guess the secret number. 
It is from 1 to 99. You have five tries. Get Guessin' !""") 

responseList = [] 

while guess != secret and tries < 5: 
    user_response = guess = easygui.integerbox ("C'mon...GUESS!!! ") 

    if not guess: break 
    if guess <= (secret + 5) and guess > secret: 
     easygui.msgbox(str(guess) + " is too HIGH... but you're close!") 
    if guess >= (secret - 5) and guess < secret: 
     easygui.msgbox(str(guess) + " is too LOW... but you're close!")   
    if guess < (secret - 5): 
     easygui.msgbox(str(guess) + " is too LOW... Guess higher")   
    if guess > (secret + 5): 
     easygui.msgbox (str(guess) + " is too HIGH...Guess lower") 

    tries = tries + 1 

    responseList.append(user_response) 
    easygui.msgbox (",".join(["%d"%x for x in responseList])) 

if guess == secret: 
    easygui.msgbox ("Darn! You got it!") 

else: 
    easygui.msgbox ("Ha, Ha, Ha! No more guesses! To the firin' squad with ya!") 
    easygui.msgbox (str(secret) + " was the secret number") 

당신이가는대로 다음 각 번호를 추가, 루프 외부 목록으로 responseList을 초기화합니다. msgbox의 숫자를 쉼표로 구분하기 위해 쉼표를 몇 개 추가했습니다. ;)

+0

easygui는 표준 라이브러리의 일부가 아니며 어디에서 가져올 지 알 수 있습니다. –

+0

좋은 점. 발견하는데 약 10 초가 걸렸지 만 다음 사람이 2에서 찾을 수 있을지도 모릅니다. –