不确定为什么我在我的程序中收到这些错误

时间:2016-12-09 00:26:02

标签: python

有谁知道为什么我在def游戏中得到一个预期的缩进块():? 另外,我在缩进时再次出现错误“y”。如果你们能给我任何关于为什么会发生这些事情的提示,如果你看到别的什么,请让我知道它会一如既往地受到高度赞赏。

import random 
def main():
    player1Score = 0
    compScore = 0 

    Intro()
    displayMenu()


def Intro():
    print("Welcome to the League of Rock, Paper, Scissors!")
    print("Where contestants battle it out to see who is the best")


    print("Main Menu")
    print("1 See the rules")
    print("2 play against the computer")
    print("3 play a two player game")
    print("4 Quit")
    decision = int(input("\nEnter choice:"))

    #Input validation
    while decision <= 0 or decision > 4:
        print("'ERROR: You must choose 1,2,3 or 4")
        decision = int(input("Enter your choice:"))

    if decision == 1:
        print("The rules here are simple")
        print("Paper Covers Rock")
        print("Rock smashed Scissors")
        print("Scissors Cut Paper")

    if decision == 4:
        print("Goodbye..Thank you for playing")
    if decision == 2:
        return game()

    if decision == 3:
        return player()

def game():
    player_choice = raw_input('Do you choose rock [r], paper [p], or scissors [s]? ')

    computer_choice = randint(0,2)
    #Rock = 0 Paper = 1 Scissors = 2

    #Player chooses paper, computer chooses rock
    if player_choice == "p" and computer_choice == 0:
        print("Computer chose rock")
        player_won()

    #Player chooses rock, computer chooses scissors
    elif player_choice == 'r' and computer_choice == 2:
        print("Computer chose scissors")
        player_won()

    #Player chooses scissors, computer chooses paper
    elif player_choice == 's' and computer_choice == 1:
        print("Computer chose paper")
        player_won()

    #Computer chooses paper, player chooses rock
    elif player_choice == 'r' and computer_choice == 1:
        print("Computer chose paper")
        computer_won()

    #Computer chooses rock, player chooses scissors
    elif player_choice == 's' and computer_choice == 0:
        print("Computer chose rock")
        computer_won()

    #Computer chooses scissors, player chooses paper
    elif player_choice == 'p' and computer_choice == 2:
        print("Computer chose scissors")
        computer_won()

    #Ties
    elif player_choice == 'r' and computer_choice == 0:
        print("It's a tie!")
        game()

    elif player_choice == 's' and computer_choice == 2:
        print("It's a tie!")
        game()

    elif player_choice == 'p' and computer_choice == 1:
        print("It's a tie!")
        game()

    #Wrong input
    else:
        print("Please try again.")
        game()

def player_won():
    global player_count
    print("You win!")
    player_count += 1
    print("You have ' + str(player_count) + ' point(s).")
    if player_count < 3:
        game()

def computer_won():
    global comp_count
    print("Computer wins!")
    comp_count += 1
    print("Computer has ' + str(comp_count) + ' point(s).")
    if comp_count < 3:
        game()

print("Welcome to Rock, Paper, Scissors! First to 3 points wins it all.")
game()

def player():
   ready = input("Requires two players; Are you ready to play? (y/n) ")
while ready != 'y' and ready != 'n':
    ready = input("Oops, I didn't quite get that.")

def player_1():
     x = input("Player 1 - Pick your mode of attack! ")
     while x != 'Rock'and x !='rock' and x != 'Paper'and x != 'paper'and x != 'scissors'and x !='Scissors':
         x = input("Oops, that won't do you any good... ")
     return x

def player_2():
    x = input("Player 2 - Pick your mode of attack! ")
    while x != "Rock" and x != "rock" and x != "Paper" and x != "paper" and x != 'scissors'and x != 'Scissors':
        x = input("Oops, that won't do you any good... ")
    return x 
def play_again():
    play_again = input("Would you guys like to play again? (y/n) ")
    while play_again != "y" and play_again != "n":
        play_again = input("I'm sorry, what was that? (y/n) ")
            if play_again == "y":
                    return check()
        elif play_again == "n":
            print "Thanks for playing!"

def check():
    Score1 = 2
    Score2 = 2
    while Score1 > 0 and Score2 > 0:
        P1 = player_1()
        P2 = player_2()
        if P1.lower() == P2.lower():
            print("it's a tie!")
        elif P1.lower() == "rock" and P2.lower() == "scissors":
            print("Player 1 Wins!")
            Score1 -= 1
        elif P1.lower() == "paper" and P2.lower() == "rock":
            print("Player 1 Wins!")
            Score1 -= 1
        elif P1.lower() == "scissors" and P2.lower() == "paper":
            print("Player 1 Wins!")
            Score1 -= 1
        else:
            print("Player 2 Wins!")
            Score2 -= 1

        if Score1 == 0:
            print("Congratulations Player 1 for winning best out of three!")
        if Score2 == 0:
            print("Congratulations Player 2 for winning best out of three!")
    play_again()        
check()       


main()

1 个答案:

答案 0 :(得分:0)

非常确定您不应该使用elif并使用if,因为它上面没有if个语句。

另外,我认为您不需要在功能中返回H1。