2017-11-22 3 views
0

나는 바위, 종이 또는 가위를 연주하도록 선택할 수있는 프로그램을 만들려고합니다. 일단 그들이 무엇을하고 싶은지와 그들이하고 싶은 게임의 양을 선택하면, 그들이이기 든 잃든 지 말하게됩니다.가위 바위 가위 결과

나는 그들이 얻거나 잃은 시간을 계산하려고하지만 대신 각 라운드 후에 인쇄합니다. 게임이 끝난 후 인쇄 할 수있는 방법이 있습니까? 내가

from random import randint 

def main(): 
    games = int(input("How many games would you like to play?")) 
    while games > 0: 
    games -= 1 
    comp = computer() 
    choice = user() 
    print("You played", choice, "and the computer played", comp) 
    winner(comp, choice) 



def computer(): 
    comp = randint in range (0,3) 
    if comp == 0: 
    comp = 'rock' 
    elif comp == 1: 
    comp = 'paper' 
    elif comp == 2: 
    comp = 'scissors' 
    return comp 

def user(): 
    choice = int(input("choose 0 for rock, 1 for paper, or 2 for scissors: ")) 
    if choice == 0: 
    choice = 'rock' 
    elif choice == 1: 
    choice = 'paper' 
    elif choice == 2: 
    choice = 'scissors' 
    else: 
    print("invalid input") 
    return choice 


def winner(comp, choice): 
    tie = 0 
    win = 0 
    lose = 0 
    while True: 
    if choice == "rock" and comp == "rock": 
     result = 'tie' 
     tie += 1 
     break 
    elif choice == 'rock'and comp == 'scissors': 
     result = "you win" 
     win += 1 
     break 
    elif choice == 'rock' and comp == 'paper': 
     result = "you lose" 
     lose += 1 
     break 
    elif choice == 'paper' and comp == 'paper': 
     result = 'tie' 
     tie += 1 
     break 
    elif choice == 'paper' and comp == 'scissors': 
     result = 'you lose' 
     lose += 1 
     break 
    elif choice == 'paper' and comp == 'rock': 
     result = 'you win' 
     win =+ 1 
     break 
    elif choice == 'scissors' and comp == 'scissors': 
     result = 'tie' 
     tie += 1 
     break 
    elif choice == 'scissors' and comp == 'paper': 
     result = 'you win' 
     win += 1 
     break 
    elif choice == 'scissors' and comp == 'rock': 
     result = 'you lose' 
     lose +=1 
     break 
    else: 
     print("error") 
     break 
    print(result) 
    print("you won", win,"times, and lost", lose,"times, and tied", tie,"times.") 


main() 
+1

이러한 질문은 모두 과제에서 비롯된 것입니까? – roelofs

+1

유용 할 경우 대답을 수락하는 것이 예의라는 것을 기억하십시오 ... – roelofs

+0

예. 당신이 그 일을 할 수 있다는 것을 깨닫지 못했습니다. – savannah

답변

0
  • win, losetie 카운터
  • randint(0, 2)에 대한 전역을 선언 마지막에 전체 결과를 사용자에게 보여주고 싶은 것은 당신이 임의의 정수
  • 그것은 +=
  • 하지 =+
  • 을 얻는 방법이다

몇 가지 실수가 있습니다. 각 줄에 # CORRECTION 주석을 추가했습니다.

from random import randint 

tie = 0 
win = 0 
lose = 0 

def main(): 
    games = int(input("How many games would you like to play?")) 
    while games > 0: 
    games -= 1 
    comp = computer() 
    choice = user() 
    print("You played", choice, "and the computer played", comp) 
    winner(comp, choice) 



def computer(): 
    # CORRECTION: randint in range (0,3) always return FALSE. BELOW IS THE CORRECT WAY 
    comp = randint(0, 2) 
    if comp == 0: 
    comp = 'rock' 
    elif comp == 1: 
    comp = 'paper' 
    elif comp == 2: 
    comp = 'scissors' 
    return comp 

def user(): 
    choice = int(input("choose 0 for rock, 1 for paper, or 2 for scissors: ")) 
    if choice == 0: 
    choice = 'rock' 
    elif choice == 1: 
    choice = 'paper' 
    elif choice == 2: 
    choice = 'scissors' 
    else: 
    print("invalid input") 
    return choice 


def winner(comp, choice): 
    # CORRECTION: MAKE ALL THE COUNTERS GLOBAL 
    global tie 
    global win 
    global lose 

    while True: 
    if choice == "rock" and comp == "rock": 
     result = 'tie' 
     tie += 1 
     break 
    elif choice == 'rock'and comp == 'scissors': 
     result = "you win" 
     win += 1 
     break 
    elif choice == 'rock' and comp == 'paper': 
     result = "you lose" 
     lose += 1 
     break 
    elif choice == 'paper' and comp == 'paper': 
     result = 'tie' 
     tie += 1 
     break 
    elif choice == 'paper' and comp == 'scissors': 
     result = 'you lose' 
     lose += 1 
     break 
    elif choice == 'paper' and comp == 'rock': 
     result = 'you win' 
     # CORRECTION: ITS NOT =+ ITS += 
     # win =+ 1 
     win += 1 
     break 
    elif choice == 'scissors' and comp == 'scissors': 
     result = 'tie' 
     tie += 1 
     break 
    elif choice == 'scissors' and comp == 'paper': 
     result = 'you win' 
     win += 1 
     break 
    elif choice == 'scissors' and comp == 'rock': 
     result = 'you lose' 
     lose +=1 
     break 
    else: 
     print("error") 
     break 
    print(result)  


main() 
print("you won", win,"times, and lost", lose,"times, and tied", tie,"times.")