简体   繁体   中英

I wrote a simple python game but functions in that are not executing

I am fairly new in python coding. I made a simple rock, paper, scissor game using random module. I wrote it in three functions. when I am executing it, I get no error but it is not executing the functions.

what I wrote:

import random

def main():
     global comp
     global user
     global play

     play = ''

     #preparing list for computer to select from
     game = ['rock', 'paper', 'scissor']

     #computer chooses randomly from the list above
     comp = random.choice(game)
     print(comp)

     #user inputs their choice
     user = input("Rock, Paper or Scissor? ")

def user_choice():
     global play
     play = input("Do you want to continue? press y or n for yes and no respectively. ")

     if play == y:
         main()
     elif play == n:
         print("Thank you for playing!")
         exit()

 #conditions to the game
def play_game():
     global user
     global comp

     while True:    
        if user == comp:
           print("Tie!")
           print("Computer: ", comp, "and User:", user)

        elif user == 'rock':
            if comp == 'paper':
               print("Rock covers paper, computer wins")

            else:
               print("rock smashes through scissor, you win")

        elif user == 'paper':
            if comp == 'rock':
               print("paper covers rock, you win!")

            else:
               print("scissor cuts through paper, computer wins")

        elif user == 'scissor':
            if comp == 'rock':
               print("Rock smashes through scissor, Computer wins!")

            else:
               print("Scissor cuts through paper")

       user_choice()

if __name__ == '__main__':
    main()

play_game()

it is not executing play_game() and user_choice(). It only ask for the input.

The code calls the main() function and this takes an input, but it does not call any of the other functions.

This is why.

Expect that you mean to call play_game() and user_choice() from within main() .

Working code.

import random

def main():
     global comp
     global user
     global play

     play = ''

     #preparing list for computer to select from
     game = ['rock', 'paper', 'scissor']

     #computer chooses randomly from the list above
     comp = random.choice(game)
     print(comp)

     #user inputs their choice
     user = input("Rock, Paper or Scissor? ")

     user_choice()
     play_game()
     

def user_choice():
     global play
     play = input("Do you want to continue? press y or n for yes and no respectively. ")

     if play == 'y':
         main()
     elif play == 'n':
         print("Thank you for playing!")
         exit()

#conditions to the game
def play_game():
     global user
     global comp

     while True:    
        if user == comp:
           print("Tie!")
           print("Computer: ", comp, "and User:", user)

        elif user == 'rock':
            if comp == 'paper':
               print("Rock covers paper, computer wins")

            else:
               print("rock smashes through scissor, you win")

        elif user == 'paper':
            if comp == 'rock':
               print("paper covers rock, you win!")

            else:
               print("scissor cuts through paper, computer wins")

        elif user == 'scissor':
            if comp == 'rock':
               print("Rock smashes through scissor, Computer wins!")

            else:
               print("Scissor cuts through paper")


       

if __name__ == '__main__':
    main()

Create another function main() and call play_game() and user_choice() within main.

The first thing which is wrong is that y and n are not under "" thus python is treating them as a variable. the second thing which was wrong is the position of the play game which should come below the main() to get executed. All user_choice() was not indented properly Try this code

import random


def main():
     global comp
     global user
     global play

     play = ''

     # preparing list for computer to select from
     game = ['rock', 'paper', 'scissor']

     # computer chooses randomly from the list above
     comp = random.choice(game)
     print(comp)

     # user inputs their choice
     user = input("Rock, Paper or Scissor? ")


def user_choice():
     global play
     play = input(
         "Do you want to continue? press y or n for yes and no respectively. ")

     if play == "y":
         main()
     elif play == "n":
         print("Thank you for playing!")
         exit()

 # conditions to the game


def play_game():
     global user
     global comp

     while True:
        if user == comp:
           print("Tie!")
           print("Computer: ", comp, "and User:", user)

        elif user == 'rock':
            if comp == 'paper':
               print("Rock covers paper, computer wins")

            else:
               print("rock smashes through scissor, you win")

        elif user == 'paper':
            if comp == 'rock':
               print("paper covers rock, you win!")

            else:
               print("scissor cuts through paper, computer wins")

        elif user == 'scissor':
            if comp == 'rock':
               print("Rock smashes through scissor, Computer wins!")

            else:
               print("Scissor cuts through paper")

        user_choice()

if __name__ == '__main__':
    main()
    play_game()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM