I created a Python function, but it is not executed

Asked

Viewed 387 times

-1

I was studying about the Python language and, as a test, I played a game of Jokenpô.

To make it restart, I saw that I would have to put it inside a function to eventually return to it and restart the program, but for some reason, when I put the program inside a function it just doesn’t return anything when I run it.

Below I show the program in the current state, the problem started to occur when I inserted the program into a function.

#Jokenpô 
from random import randint
import time

def jokenpo():
    player = int(input('Digite 1 para PEDRA \nDigite 2 para PAPEL \nDigite 3 para TESOURA\n'))
    machine = randint(1, 3)

    if player == 1:
        print('Jo')
        time.sleep(1)
        print('Ken')
        time.sleep(1) 
        print('Pô!')
        print('Você escolheu pedra.')
        if machine == 1:
            print('A máquina escolheu Pedra, vocês empataram.')
        elif machine == 2:
            print('A máquina escolheu Papel, você perdeu.')
        elif machine == 3:
            print('A máquina escolheu Tesoura, você ganhou')
    if player == 2:
        print('Jo')
        time.sleep(1)
        print('Ken')
        time.sleep(1)
        print('Pô!')
        print('Você escolheu papel.')
        if machine == 1:
            print('A máquina escolheu Pedra, você ganhou.')
        elif machine == 2:
            print('A máquina escolheu Papel, vocês empataram.')
        elif machine == 3:
            print('A máquina escolheu Tesoura, você perdeu.')
    if player == 3:
        print('Jo')
        time.sleep(1)
        print('Ken')
        time.sleep(1)
        print('Pô!')
        print('Você escolheu tesoura')
        if machine == 1:
            print('A máquina escolheu Pedra, você perdeu.')
        elif machine == 2:
            print('A máquina escolheu Papel, você ganhou.')
        elif machine == 3:
            print('A máquina escolheu Tesoura, vocês empataram')
  • 1

    And when you called the function jokenpo to execute it?

  • What do you mean? How do I call it? My goal is to make the program give you the option to restart you know, type a 'Want to play again? '' and do not close the prompt once the execution is finished.

  • Do you know what a function is and how it works? You just declared the function, but it will not run until you call it. Read more on Defining functions.

  • Ah yes, I must call her after the statement, had completely ignored it. Thank you.

3 answers

5

Ridolfi, I’ve run the code around here and it’s all quiet, I believe what’s missing is two pieces of information that would be pertinent to your code:

  • include the # coding:utf-8 in the script header: because you are using some special characters in the course of the code. And this is the pattern that we have from python which is responsible for recognising them, and can actually present some when performing the execution.

  • Another important thing is you call the function jokenpo() that created, a useful way to do this, is to use the following code scope:

    if __name__ == "__main__": 
         jokenpo()
    

By executing the same code you posted here, with the changes I made, I got the result in the console:

Digite 1 para PEDRA
Digite 2 para PAPEL
Digite 3 para TESOURA
2
Jo
Ken
Pô!
Você escolheu papel.
A máquina escolheu Papel, vocês empataram.
  • 1

    Got it! Thank you

  • Encoding information is only required in Python 2. For version 3, UTF-8 is assumed by default, see PEP 3120.

3

The error that causes your code to not run is that you have never called the function you declared. Add to end of file to resolve:

if __name__ == "__main__": 
     jokenpo()

See more about function definitions in Defining functions in the official documentation.

However, there is a lot of repeated code; notice that your program has 3 conditions and the code within them is practically the same. There are ways to simplify this. For example, you can define a enumeration to represent the game options:

from enum import Enum

class Jokenpo(Enum):
    pedra = 1
    papel = 2
    tesoura = 3

You ask the player his option and draw the computer’s option:

voce = Jokenpo(int(input('Número de 1 a 3: ')))
computador = Jokenpo(randint(1, 3))

Check who won:

if voce.value > computador.value or (voce.value, computador.value) == (1, 3):
    resultado = 'venceu'
elif voce.value == computador.value:
    resultado = 'empatou'
else:
    resultado = 'perdeu'

And finally displays the result:

print(f'Você {resultado} escolhendo {voce.name} contra {computador.name}')

The code, then, would be:

from enum import Enum
from random import randint

class Jokenpo(Enum):
    pedra = 1
    papel = 2
    tesoura = 3

voce = Jokenpo(int(input('Número de 1 a 3: ')))
computador = Jokenpo(randint(1, 3))

if voce.value > computador.value or (voce.value, computador.value) == (1, 3):
    resultado = 'venceu'
elif voce.value == computador.value:
    resultado = 'empatou'
else:
    resultado = 'perdeu'

print(f'Você {resultado} escolhendo {voce.name} contra {computador.name}')

See working on Repl.it

Note that to define who won I checked who chose the highest value; 2 (paper) wins 1 (stone), while 3 (scissors) wins 2 (paper). The only exception is 1 (stone) winning 3 (scissors), which I made manually.

Would be:

>>> Número de 1 a 3: 3
Você venceu escolhendo tesoura contra papel

0

You have defined the function jokenpo() but it didn’t call, so it won’t run until it’s called. Add the following at the end of your code:

if __name__ == "__main__": 
    jokenpo()

The code fragment above serves precisely to call the function. From the moment I added here, the code ran normally, as can be seen in the following image:

inserir a descrição da imagem aqui

The code went like this:

#Jokenpô 
from random import randint
import time
def jokenpo():
    player = int(input('Digite 1 para PEDRA \nDigite 2 para PAPEL \nDigite 3 para TESOURA\n'))
    machine = randint(1, 3)
    if player == 1:
        print('Jo')
        time.sleep(1)
        print('Ken')
        time.sleep(1) 
        print('Pô!')
        print('Você escolheu pedra.')
        if machine == 1:
            print('A máquina escolheu Pedra, vocês empataram.')
        elif machine == 2:
            print('A máquina escolheu Papel, você perdeu.')
        elif machine == 3:
            print('A máquina escolheu Tesoura, você ganhou')
    if player == 2:
        print('Jo')
        time.sleep(1)
        print('Ken')
        time.sleep(1)
        print('Pô!')
        print('Você escolheu papel.')
        if machine == 1:
            print('A máquina escolheu Pedra, você ganhou.')
        elif machine == 2:
            print('A máquina escolheu Papel, vocês empataram.')
        elif machine == 3:
            print('A máquina escolheu Tesoura, você perdeu.')
    if player == 3:
        print('Jo')
        time.sleep(1)
        print('Ken')
        time.sleep(1)
        print('Pô!')
        print('Você escolheu tesoura')
        if machine == 1:
            print('A máquina escolheu Pedra, você perdeu.')
        elif machine == 2:
            print('A máquina escolheu Papel, você ganhou.')
        elif machine == 3:
            print('A máquina escolheu Tesoura, vocês empataram')
if __name__ == "__main__": 
    jokenpo()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.