Python Forum
i want to shorten a working section
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
i want to shorten a working section
#9
i did the value_type thing two on my own yay


heres the entire thing i have now
import random
werte = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
durchschnitt2 = False
def int_input_de(prompt, type, max_value=None, min_value=None):
    while True:
        try:
            value = int(input(prompt))
            if max_value is not None and value > max_value:
                print(f"maximal {max_value} {type}")
            elif min_value is not None and value < min_value:
                print(f"minimal {min_value} {type}")
            else:
                return value
        except ValueError:
            print("ungültiger wert. versuche es noch einmal")
            print("----------------------------------")

def deutsch():
    runig = True
    while runig:
        runden = int_input_de("wie viele runden wilst du speilen?:", "runden", max_value=15)
        for runde in range (runden):
            runde = runde + 1
            print ("----------------------------------")
            print (f"runde {runde}")
            while True:
                low = int_input_de("was soll das minimum sein?:", "", max_value=None, min_value=-500000)
                print ("----------------------------------")
                high = int_input_de("was soll das maximum sein?:", "", max_value=500000)
                if low > high:
                    print ("----------------------------------")
                    print("das minimum kann nicht mehr als das maximum sein")
                    print ("----------------------------------")
                else:
                    break
            print ("----------------------------------")
            number = random.randint(low,high)
            if not durchschnitt2:
                guesses = 0
                guessesnuber = int_input_de("wie viele versuch wilst du haben?:", "versuche", max_value=100)                    
                print ("----------------------------------")
                print (f"du hast {guessesnuber} versuche")
            else:
                guesses = 0
                guessesnuber = 9999999999999999999999999999
                print ("----------------------------------")
                print (f"du hast {guessesnuber} versuche")
            while True:
                guess = int_input_de(f"nummer zwischen ({low} - {high}):", "")

                guesses = guesses+1
            
                if guess > high:
                    print (f"das ist mehr als {high}")
                elif guess < low:
                    print(f"das ist weniger als {low}")
                elif guess > number:
                    print (f"{guess} ist zu viel")
                elif guess < number:
                    print (f"{guess} ist zu wenig") 
                else:
                    print (f"{guess} ist richtig!")
                    break
                if guesses == guessesnuber:
                    print (f"du hast zu viele versuche gebraucht")
                    break
                else:
                    print (f"du hast noch {guessesnuber-guesses} versuche")
                if guess < number and not guess < low:
                   low = guess
                elif guess > number and not guess > high:
                    high = guess
                print ("----------------------------------")
            if guess == number:
                print (f"du hast {guesses} versuche gebraucht")
                if durchschnitt2:
                    if 1 <= runde <= 15:
                        werte[runde-1] = guesses
                        if runde == 1:
                            durchschnitt1 = guesses
                        else:
                            durchschnitt1 += guesses
                    
            elif guesses == guessesnuber:
                print (f"du hast es nicht eraten die numer war {number}")
        if durchschnitt2:
            durchschnitt = durchschnitt1/runden    
            print (f"dein durchschnitt ist {durchschnitt}")
        if not input ("nochmal spielen? (ja/nein):").lower() == "ja":
            runig = False
        else:
            print ("----------------------------------")
def englisch():
    runig = True
    while runig:
        runden = int_input("how many rounds woud you like to play?:", "rounds", max_value=15)
        for runde in range (runden):
            runde = runde + 1
            print ("----------------------------------")
            print (f"round {runde}")
            while True:
                low = int_input("waht woud you like to be the minimum?:", "", max_value=None, min_value=-500000)
                print ("----------------------------------")
                high = int_input("waht woud you like to be the maximum?:", "", max_value=500000)
                if low > high:
                    print ("----------------------------------")
                    print("das minimum kann nicht mehr als das maximum sein")
                    print ("----------------------------------")
                elif high < low:
                    break
            print ("----------------------------------")
            number = random.randint(low,high)
            if not durchschnitt2:
                guesses = 0
                guessesnuber = int_input("wie viele versuch wilst du haben?:", "versuche", max_value=100)                    
                print ("----------------------------------")
                print (f"du hast {guessesnuber} versuche")
            else:
                guesses = 0
                guessesnuber = 9999999999999999999999999999
                print ("----------------------------------")
                print (f"du hast {guessesnuber} versuche")
            while True:
                guess = int_input(f"nummer zwischen ({low} - {high}):", "")

                guesses = guesses+1
            
                if guess > high:
                    print (f"das ist mehr als {high}")
                elif guess < low:
                    print(f"das ist weniger als {low}")
                elif guess > number:
                    print (f"{guess} ist zu viel")
                elif guess < number:
                    print (f"{guess} ist zu wenig") 
                else:
                    print (f"{guess} ist richtig!")
                    break
                if guesses == guessesnuber:
                    print (f"du hast zu viele versuche gebraucht")
                    break
                else:
                    print (f"du hast noch {guessesnuber-guesses} versuche")
                if guess < number and not guess < low:
                   low = guess
                elif guess > number and not guess > high:
                    high = guess
                print ("----------------------------------")
            if guess == number:
                print (f"du hast {guesses} versuche gebraucht")
                if durchschnitt2:
                    if 1 <= runde <= 15:
                        werte[runde-1] = guesses
                        if runde == 1:
                            durchschnitt1 = guesses
                        else:
                            durchschnitt1 += guesses
                    
            elif guesses == guessesnuber:
                print (f"du hast es nicht eraten die numer war {number}")
        if durchschnitt2:
            durchschnitt = durchschnitt1/runden    
            print (f"dein durchschnitt ist {durchschnitt}")
        if not input ("nochmal spielen? (ja/nein):").lower() == "ja":
            runig = False
        else:
            print ("----------------------------------")
def italienisch():
    runig = True
    while runig:
        while True:
            runden = int(input("quanti giri vuoi fare?:"))
            if runden > 20:
                print ("20 giri massimo")
            else:
                break

        for runde in range (runden):
            runde = runde + 1
            print ("----------------------------------")
            print (f"giro {runde}")
            while True:
                while True:
                    low = int(input("Quanto vuoi mettere per il minimo?:"))
                    if low < -500000:
                        print ("al minimo -500,000")
                    else:
                        break
                print ("----------------------------------")
                while True:    
                    high = int(input("quanto vuoi mettere come il massimo?:"))
                    if high > 500000:
                        print ("massimo 500,000")
                    else:
                        break
                if low > high:
                    print ("----------------------------------")
                    print("il minimo non può essere più che il massimo")
                    print ("----------------------------------")
                else:
                    break
            print ("----------------------------------")
            guesses = 0
            number = random.randint(low,high)
            guessesnuber = int(input("quanti tentativi vuoi avere?:"))
            print ("----------------------------------")
            print (f"hai {guessesnuber} tentativi")
            while True:
                guess = int(input(f"numero fra ({low} - {high}):"))
                guesses = guesses+1
        
                if guess > high:
                    print (f"è più che {high}")
                elif guess < low:
                    print(f"è meno che  {low}")
                elif guess > number:
                    print (f"{guess} è troppo alto")
                elif guess < number:
                    print (f"{guess} è troppo basso") 
                else:
                    print (f"{guess} è giusto!")
                    break
                if guesses == guessesnuber:
                    print (f"hai fatto troppi tentativi")
                    break
                else:
                    print (f"hai ancora {guessesnuber-guesses} tentativi")
                if guess < number and not guess < low:
                    low = guess
                elif guess > number and not guess > high:
                    high = guess
                print ("----------------------------------")
            if guess == number:
                print (f"in questo giro hai avuto bisogno di {guesses} tentativi")
            elif guesses == guessesnuber:
                print (f"non l'hai indovinato, il numero era: {number}") 
        if not input ("gioca di nuovo? (sì/no):").lower() == "si":
            runig = False
        else:
            print ("----------------------------------")
while True:
    sprache = input("language?(eng/de/it):").lower()
    if sprache == "eng":
        print ("the game is now in english")
        if input ("do you want to calculate an average? (you will have infinite trys)(yes/no):").lower() == "yes":
            durchschnitt2 = True   
        englisch()
        break
    elif sprache == "de":
        print ("das spiel is jetzt auf deutsch")
        if input ("wilst du einen durchschnitt berechnen? (du wirst unendlich viele versuche haben)(ja/nein):").lower() == "ja":
            durchschnitt2 = True 
        deutsch()
        break
    elif sprache == "it":
        print ("il gioco continua in italiano")
        italienisch()
        break
    else:
        print('please enter a valid language')
        print ("---------------------------") 
Reply


Messages In This Thread
i want to shorten a working section - by pizzakafz - Aug-19-2024, 03:18 PM
RE: i want to shorten a working section - by pizzakafz - Aug-20-2024, 07:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  If I need to do regex replacement 27000 times, how to shorten it? tatahuft 4 877 Dec-26-2024, 01:51 AM
Last Post: deanhystad
  How to add multi-line comment section? Winfried 2 1,410 Jun-04-2024, 07:24 AM
Last Post: Gribouillis
  python multiple try except block in my code -- can we shorten code mg24 10 15,549 Nov-10-2022, 12:48 PM
Last Post: DeaD_EyE
  Want to shorten the python code shantanu97 3 2,123 Apr-25-2022, 01:12 PM
Last Post: snippsat
  Append data to Yaml section tbaror 0 8,865 Feb-09-2022, 06:56 PM
Last Post: tbaror
  Create an isolated section in bash? yxk 1 2,620 Feb-27-2020, 11:16 AM
Last Post: Larz60+
  Best section to do a while loop in my code mrapple2020 0 2,166 Apr-15-2019, 01:14 AM
Last Post: mrapple2020
  ConfigParser.NoSectionError: No section: 'Default' degenaro 1 16,495 Jun-08-2018, 08:33 PM
Last Post: ljmetzger
  How to shorten the size of program garikhgh0 11 8,205 Feb-22-2018, 06:35 PM
Last Post: snippsat
  Help understanding code section raz631 4 4,190 Dec-14-2017, 09:32 PM
Last Post: raz631

Forum Jump:

User Panel Messages

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