openlang

이 랜덤 선택기는 왜 B를 자꾸 보여 주나요?

이 프로그램의 랜덤 선택기가 B를 연달아 길게 보여 주곤 해요. 결과가 좀 더 다양하게 나오게 할 방법이 있나요?


import random
import pyglet
#opens window
screen = pyglet.window.Window(fullscreen=True)
caption = pyglet.text.Label('press A',
                          font_name='Times New Roman',
                          font_size=72,
                          x=screen.width//2, y=screen.height//2,
                          anchor_x='center', anchor_y='center')
alternate= pyglet.text.Label('press B',
                          font_name='Times New Roman',
                          font_size=72,
                          x=screen.width//2, y=screen.height//2,
                          anchor_x='center', anchor_y='center')
def refresh(elapsed):
    pick=random.SystemRandom().randint(1,2)
    if pick==1:
        screen.clear()
        caption.draw()
    else:
        screen.clear()
        alternate.draw()
pyglet.clock.schedule_interval(refresh,pick)
pyglet.app.run()
영어에서 번역원문 보기

이어지는 대화 2

서로의 근거를 따라
  1. 고유 링크

    간격을 줄이면 더 다양하게 나오네요. 예를 들어 0.2요

    
    pyglet.clock.schedule_interval(refresh, 0.2)

    각 라벨이 표시되는 시간이 줄어드니까요.

    schedule_interval()의 pick에는 문제가 있어요. pick은 refresh() 안의 지역 변수라서 함수 밖에서는 쓸 수 없으므로 NameError: name 'pick' is not defined가 나와요.

    영어에서 번역원문 보기
  2. 고유 링크

    변수 pick이 pyglet.clock.schedule_interval을 호출하는 범위에 없어서 이 예제는 실행되지 않아요

    어떤 의도인지 잘 모르겠네요. 프로그램을 실행할 때마다 간격이 달라져야 하나요? 간격은 고정하는 편이 더 자연스러워요.

    구조를 조금 정리하면 도움이 될 것 같아요.

    런타임 동작이 좀 이상하긴 한데, pyglet의 특성일 수도 있고 제가 잘 이해하지 못해서일 수도 있어요. 어쨌든 이 코드는 "Press A"와 "Press B"를 의사 난수 빈도로 번갈아 표시해요.

    
    from random import SystemRandom
    from pyglet import window, text, clock, app
    
    # pylint: disable=possibly-used-before-assignment,unused-argument
    
    # note use of variable names in uppercase denoting global scope
    
    TICK_SECONDS = 3.
    
    def refresh(elapsed: float) -> None:
        SCREEN.clear()
        RNG.choice(CAPTIONS).draw()
    
    if __name__ == "__main__":
        SCREEN = window.Window(fullscreen=True)
        label_options = {  # these parameters are common to both captions
            "font_name": "Times New Roman",
            "font_size": 72,
            "x": SCREEN.width // 2,
            "y": SCREEN.height // 2,
            "anchor_x": "center",
            "anchor_y": "center",
        }
        # create a tuple of the 2 captions
        CAPTIONS = text.Label("Press A", **label_options), text.Label("Press B", **label_options)
        RNG = SystemRandom()  # only do this once
        clock.schedule_interval(refresh, TICK_SECONDS)
        app.run()
    영어에서 번역원문 보기

같은 질문의 다른 갈래

  1. Java Mail에서 이메일 주소 대신 발신자 이름을 표시하기

    Java Mail API로 메시지를 보내고 있어요. 도착한 메일의 받은편지함에는 보낸 주소가 표시돼요. 그 발신자 주소 대신 텍스트를 보여 주고 싶어요. facebook에서 오는 메일은 받은편지함에 "mailbox@faceb…

    답글 2최근 활동 java
  2. TypeScript에서 Dynamics CRM에 접근하기

    CRM 코드가 웹 리소스 안에서 실행되다 보니 엔터티 데이터를 가져오려면 이렇게 작성해야 해요. 그런데 typescript로 작성하면 이 표현식이 컴파일되지 않아요. 지금은 Xrm.Page 앞에 window.parent를..…

    답글 2최근 활동 typescript
  3. Gleam HTTP 응답 헤더에는 어떤 날짜 형식이 필요한가요?

    Date 헤더를 보내고 싶어요. 필요한 형식은 다음과 같아요: 지금까지 시도한 코드는 이거예요:

    답글 3최근 활동 gleam