Python Forum
help on def in if block - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: help on def in if block (/thread-33496.html)



help on def in if block - jip31 - Apr-30-2021

Hi
I dont' understandard why the tirage() def doesnt execute (it works only after else block)
Could you help please??

import random

def tirage():
       for i in range(6):
              print(random.randint(1, 40))

print("Voulez-vous jouer au loto", "?")
reponse= input("Entrez votre réponse:")
print("Ma réponse est: " + reponse)
if reponse == 'oui':
 print("Super,le tirage va commencer" '!')
tirage()
else:
  print ("Tant pis, bonne soirée...")



RE: help on def in if block - DPaul - Apr-30-2021

It looks like there is something wrong
with the indentation, it is inconsistent.
Especially lines 11-12 compared to 4-5
When fixed everything works fine.
Paul


RE: help on def in if block - jip31 - Apr-30-2021

(Apr-30-2021, 06:59 AM)DPaul Wrote: It looks like there is something wrong
with the indentation, it is inconsistent.
Especially lines 11-12 compared to 4-5
When fixed everything works fine.
Paul

sure but what is the better whay to check identation without use REPL
which code editor I have to use for good identation?


RE: help on def in if block - DPaul - Apr-30-2021

The simplest editor will do.
Even IDLE will tell you that something in the syntax is not right.
You can also count TABs , can't go wrong.

But nothing will verify the logic of the indentation,
that is entirely up to you.

Paul


RE: help on def in if block - jip31 - Apr-30-2021

(Apr-30-2021, 08:02 AM)DPaul Wrote: The simplest editor will do.
Even IDLE will tell you that something in the syntax is not right.
You can also count TABs , can't go wrong.

But nothing will verify the logic of the indentation,
that is entirely up to you.

Paul

I try to use tab in many different way but impossible to do my code working!!!


RE: help on def in if block - jip31 - Apr-30-2021

now indentation is good

import random

def tirage():
    for i in range(6):
        print(random.randint(1, 40))


print("Voulez-vous jouer au loto", "?")
reponse= input("Entrez votre réponse:")
print("Ma réponse est: " + reponse)
if reponse == 'Oui' or reponse == 'oui':
 print("Super,le tirage va commencer" '!')

else:
  print ("Tant pis, bonne soirée...")
tirage()

input('press enter to continue')
but if I put the tirage() function in the if block instead the else block, I have the message SyntaxError: invalid syntax
how use this function in the if block?

And I also noted that if I delete the else block and put the function in the if block it works
import random

def tirage():
    for i in range(6):
        print(random.randint(1, 40))
    return i

print("Voulez-vous jouer au loto", "?")
reponse= input("Entrez votre réponse:")
print("Ma réponse est: " + reponse)
if reponse == 'Oui' or reponse == 'oui':
 print("Super,le tirage va commencer" '!')
tirage()



RE: help on def in if block - ibreeden - Apr-30-2021

Hi Jip,
I see no problem in calling the function from the if block. I think your indentation is still not correct. All the lines from the "if" block must have the same indentation (preferably 4 spaces).
import random

def tirage():
    for i in range(6):
        print(random.randint(1, 40))

print("Voulez-vous jouer au loto", "?")
reponse = input("Entrez votre réponse:")
print("Ma réponse est: " + reponse)
if reponse == 'Oui' or reponse == 'oui':
    print("Super,le tirage va commencer" '!')
    tirage()
else:
    print("Tant pis, bonne soirée...")

input('press enter to continue')
Output:
Voulez-vous jouer au loto ? Entrez votre réponse:oui Ma réponse est: oui Super,le tirage va commencer! 27 7 1 33 25 13 press enter to continue



RE: help on def in if block - jip31 - Apr-30-2021

(Apr-30-2021, 10:50 AM)ibreeden Wrote: Hi Jip,
I see no problem in calling the function from the if block. I think your indentation is still not correct. All the lines from the "if" block must have the same indentation (preferably 4 spaces).
import random

def tirage():
    for i in range(6):
        print(random.randint(1, 40))

print("Voulez-vous jouer au loto", "?")
reponse = input("Entrez votre réponse:")
print("Ma réponse est: " + reponse)
if reponse == 'Oui' or reponse == 'oui':
    print("Super,le tirage va commencer" '!')
    tirage()
else:
    print("Tant pis, bonne soirée...")

input('press enter to continue')
Output:
Voulez-vous jouer au loto ? Entrez votre réponse:oui Ma réponse est: oui Super,le tirage va commencer! 27 7 1 33 25 13 press enter to continue

hi ibredeen
you are right
there was just an empty line before else in my code....