vendredi 1 janvier 2021

Python if condition optimization

Hi i wrote a little easy python code to help my girlfriend learn her psy lessons but i made a condition that looks so UGLY !!!

I mean all lines looks pretty the same, so maybe there would be a way to regroup the same looking stuff, maybe with a loop ?

I was wondering if there is somehow a way to optimize it...?

Here's the code

import random
import sys
import time

list_domaine=["Communication","Reciprocal social interaction","Communication and social"]
list_autism=["Neurotypical","Borderline","Autist"]
    
def intro():
    global player_name
    
    player_name=input("What's your name ?\n")
    print("Hi "+player_name+" !\n"+"Welcome to ADOS-2 'Autism trainer' V1.0 !!!\n")
    time.sleep(3)
    print("It's time for you to start your training !\n")
    time.sleep(3)
    print("Oh wait "+player_name+" !")
    while True:
        try:
            player_age=int(input("How old are you ?\n"))
            break
        except ValueError:
            print("Tell me your age please !\n")  
            continue
    if player_age>30:
        print("Wow ! Aren't you too old to be a psychologist ?! Nah ! I'm just kidding you "+player_name+"!\n")
    else:
        print("Wow ! Aren't you too young to be a psychologist ?! Nah ! I'm just kidding you "+player_name+"!\n")
    time.sleep(3)
    print ("To tell you the truth...")
    time.sleep(3)
    print ("This app don't need your age ! ;)\n")
    time.sleep(3)
    print ("But as you told it to me, let's make it useful somehow !\nYou are "+str(player_age)+" years old.\n")
    time.sleep(3)
    print ("So... "+player_name+" Your training will begin in "+str(player_age)+" seconds !\n")
    for i in range(player_age):
        if i==player_age-1:
            print(str(player_age-i)+ " second remaining...\n")
        else:
            print(str(player_age-i)+ " seconds remaining...")
        time.sleep(1)
    build()
    

def rebuild():
    global new_builder,individu,individu2,individu3,dom1,dom2,dom3,player_name
    
    dom1=str(list_domaine[random.randrange(len(list_domaine))])
    dom2=str(list_domaine[random.randrange(len(list_domaine))])
    dom3=str(list_domaine[random.randrange(len(list_domaine))])
    individu=str(list_autism[random.randrange(len(list_autism))])
    individu2=str(list_autism[random.randrange(len(list_autism))])
    individu3=str(list_autism[random.randrange(len(list_autism))])
    if (dom1!=dom2 and dom1!=dom3 and dom2!=dom3):
        new_builder = "\nCome on "+player_name+" !\n"+"Create a person "+individu+" in "+dom1+" and "+individu2+" in "+dom2+" and "+individu3+" in "+dom3+"\n"
    else:
        rebuild()

def build():
    global new_builder,individu,individu2,individu3,dom1,dom2,dom3,player_name
    
    dom1=str(list_domaine[random.randrange(len(list_domaine))])
    dom2=str(list_domaine[random.randrange(len(list_domaine))])
    dom3=str(list_domaine[random.randrange(len(list_domaine))])
    individu=str(list_autism[random.randrange(len(list_autism))])
    individu2=str(list_autism[random.randrange(len(list_autism))])
    individu3=str(list_autism[random.randrange(len(list_autism))])

    if (dom1!=dom2 and dom1!=dom3 and dom2!=dom3):
        builder = "\nCome on"+player_name+" !\n"+"Create a person "+individu+" in "+dom1+" and "+individu2+" in "+dom2+" and "+individu3+" in "+dom3+"\n"
        print(builder)
    else:
        rebuild()
        print(new_builder)

    while True:
        try:
            reponse1 =int(input("Input a "+dom1+" score ?\n"))
            break
        except ValueError:
            print("Input a number please !\n")  
            continue
    while True:
        try:
            reponse2 =int(input("Input a "+dom2+" score ?\n"))
            break
        except ValueError:
            print("Input a number please !\n")  
            continue
    while True:
        try:
            reponse3 =int(input("Input a "+dom3+" score ?\n"))
            break
        except ValueError:
            print("Input a number please !\n")
            continue
        
    if ((((individu=="Neurotypical" and dom1=="Communication" and int(reponse1)<2)
        or (individu=="Neurotypical" and dom1=="Reciprocal social interaction" and int(reponse1)<4)
        or (individu=="Neurotypical" and dom1=="Communication and social" and int(reponse1)<7))
        and ((individu2=="Neurotypical" and dom2=="Communication" and int(reponse2)<2)
        or (individu2=="Neurotypical" and dom2=="Reciprocal social interaction" and int(reponse2)<4)
        or (individu2=="Neurotypical" and dom2=="Communication and social" and int(reponse2)<7))
        and ((individu3=="Neurotypical" and dom3=="Communication" and int(reponse3)<2)
        or (individu3=="Neurotypical" and dom3=="Reciprocal social interaction" and int(reponse3)<4)
        or (individu3=="Neurotypical" and dom3=="Communication and social" and int(reponse3)<7)))

        or (((individu=="Borderline" and dom1=="Communication" and 2<=int(reponse1)<3)
        or (individu=="Borderline" and dom1=="Reciprocal social interaction" and 4<=int(reponse1)<6)
        or (individu=="Borderline" and dom1=="Communication and social" and 4<=int(reponse1)<10))
        and ((individu2=="Borderline" and dom2=="Communication" and 2<=int(reponse2)<3)
        or (individu2=="Borderline" and dom2=="Reciprocal social interaction" and 4<=int(reponse2)<6)
        or (individu2=="Borderline" and dom2=="Communication and social" and 4<=int(reponse2)<10))
        and ((individu3=="Borderline" and dom3=="Reciprocal social interaction" and 4<=int(reponse3)<6)
        or (individu3=="Borderline" and dom3=="Communication" and 2<=int(reponse3)<3)
        or (individu3=="Borderline" and dom3=="Communication and social" and 4<=int(reponse3)<10)))

        or (((individu=="Autist" and dom1=="Communication" and int(reponse1)>=3)
        or (individu=="Autist" and dom1=="Reciprocal social interaction" and int(reponse1)>=6)
        or (individu=="Autist" and dom1=="Communication and social" and int(reponse1)>=10))
        and ((individu2=="Autist" and dom2=="Communication" and int(reponse2)>=3)
        or (individu2=="Autist" and dom2=="Reciprocal social interaction" and int(reponse2)>=6)
        or (individu2=="Autist" and dom2=="Communication and social" and int(reponse2)>=10))
        and ((individu3=="Autist" and dom3=="Communication" and int(reponse3)>=3)
        or (individu3=="Autist" and dom3=="Reciprocal social interaction" and int(reponse3)>=6)
        or (individu3=="Autist" and dom3=="Communication and social" and int(reponse3)>=10)))):

        print ("\nCongratulations "+player_name+" ! You created a person "+individu+" in "+dom1+" and "+individu2+" in "+dom2+" and "+individu3+" in "+dom3+"\n")
        time.sleep(3)
    else:
        print ("\nWhat happened "+player_name+"?! You failed your creation ! Go back to read documentation !\n")
        time.sleep(3)
        print ("I'm kind...I'll give you some hints.\n")
        time.sleep(3)
        while True:
            press=input("Type 'c' then hit 'enter' to continue...\n")
            if press=="c":
                print("\n\n#######DOCUMENTATION ON ADOS-2 SCORES#######\n\nScoring Results :\nScores relate to minimum required scores to meet the specified diagnostic cut-off in five domains.\nThe scores measure impairments, so the higher the number, the more severe the impairment, according to the test.\n\nCommunication:\nThe minimum score for autism is three, and the minimum score is two for the spectrum. If the child scores a seven, he or she would fall into 'autism' in the realm of communication.\n\nReciprocal social interaction:\nThe minimum score for autism is six, and the minimum for the spectrum is four. If the child scores a five, he or she falls into the spectrum (not necessarily classical autism) in this realm.\n\nCommunication and social:\nThe minimum score for autism is 10, and seven for the spectrum.\n\n")
                break
            else:
                print("You really need to learn more "+player_name+" ! Please press 'c' !\n")
                continue
        time.sleep(3)
    while True:
        replay=input("Replay ? (y/n)\n")
        if replay=="y":
            build()
            break
        elif replay=="n":
            sys.exit("Bye bye "+player_name+" ! Come back soon !\n")
            break
        else:
            print("Input 'y' or 'n' please !\n")
            continue

   
intro()

Thanks for help ! :)

Aucun commentaire:

Enregistrer un commentaire