Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help on def in if block
#1
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...")
Reply
#2
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
Reply
#3
(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?
Reply
#4
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
Reply
#5
(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!!!
Reply
#6
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()
Reply
#7
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
Reply
#8
(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....
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020