I created a function in Python, but it does not execute

-2

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

To make it restart, I saw that I would have to put it inside a function to in the end return to it and restart the program, but for some reason when I put the program inside a function it simply does not return anything when I squeeze it.

Below I show the program in the current state, the problem began 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')
    
asked by anonymous 04.01.2019 / 13:17

3 answers

8

Ridolfi, I ran the code here and everything is quiet, I believe that what is missing are two pieces of information that would be relevant to your code:

  • Include # coding:utf-8 in the script header: you are using some special characters throughout the code. And this is the default we have of python which is responsible for recognizing them, and can actually display some when executing.

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

    if __name__ == "__main__": 
         jokenpo()
    

When I run the same code that I posted here, with the changes I made, I got the result on 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.
    
04.01.2019 / 13:47
2

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

if __name__ == "__main__": 
     jokenpo()

See definitions of functions in Defining functions in the official documentation.

However, there is a lot of code repeated; realize that your program has 3 conditions and the code inside them is pretty much the same. There are ways to simplify this. For example, you can set a enum to represent the game's options:

from enum import Enum

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

You ask the player for his option and draw the computer:

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, it displays the result:

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

The code would then 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 at Repl.it

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

I would stay:

>>> Número de 1 a 3: 3
Você venceu escolhendo tesoura contra papel
    
04.01.2019 / 14:13
-1

You have defined the function jokenpo() but did not call it, so it will not be executed until it is called. Add the following to the end of your code:

if __name__ == "__main__": 
    jokenpo()

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

Thecodelookslikethis:

#Jokenpôfromrandomimportrandintimporttimedefjokenpo():player=int(input('Digite1paraPEDRA\nDigite2paraPAPEL\nDigite3paraTESOURA\n'))machine=randint(1,3)ifplayer==1:print('Jo')time.sleep(1)print('Ken')time.sleep(1)print('Pô!')print('Vocêescolheupedra.')ifmachine==1:print('AmáquinaescolheuPedra,vocêsempataram.')elifmachine==2:print('AmáquinaescolheuPapel,vocêperdeu.')elifmachine==3:print('AmáquinaescolheuTesoura,vocêganhou')ifplayer==2:print('Jo')time.sleep(1)print('Ken')time.sleep(1)print('Pô!')print('Vocêescolheupapel.')ifmachine==1:print('AmáquinaescolheuPedra,vocêganhou.')elifmachine==2:print('AmáquinaescolheuPapel,vocêsempataram.')elifmachine==3:print('AmáquinaescolheuTesoura,vocêperdeu.')ifplayer==3:print('Jo')time.sleep(1)print('Ken')time.sleep(1)print('Pô!')print('Vocêescolheutesoura')ifmachine==1:print('AmáquinaescolheuPedra,vocêperdeu.')elifmachine==2:print('AmáquinaescolheuPapel,vocêganhou.')elifmachine==3:print('AmáquinaescolheuTesoura,vocêsempataram')if__name__=="__main__": 
    jokenpo()
    
05.01.2019 / 01:27