diff --git a/1] guessing game/README.md b/1] guessing game/README.md new file mode 100644 index 0000000..a3c9c02 --- /dev/null +++ b/1] guessing game/README.md @@ -0,0 +1,23 @@ +## GUESSING GAME + +Symple console application for guessing random number in the range 1-100. + + +# Lauched application + +Use the "cd" command to navigate to the "1] guessing game" folder and enter the command: + +for LINUX + python3 main.py + +for WINDOWS + python main.py + + +# Running application + +1. The application generates a random number between 1 and 100. + +2. The player tries to guess this number. The application prompts if the player to enter an integer. If the player enters letters or floats, the app asks them to correct it. If the entered number is higher or lower than the selected number, the application alerts the player. If the player guesses the number, the app will tell them and the game is ending. + +3. If the player wants to quit the application during the game, he can to use keyboard "CTRL+C". \ No newline at end of file diff --git a/1] guessing game/main.py b/1] guessing game/main.py index e69de29..cdd59ad 100644 --- a/1] guessing game/main.py +++ b/1] guessing game/main.py @@ -0,0 +1,42 @@ +from random import randint + + +def num_guessing_game() -> None: + """ + The function uses randint() and generates a number in + the range 1-100. Then it uses "while loop" and it gets + number tip from the player. The functions says the player + if is his tip higher or lower until the player guesses + the number. + """ + print("Welcome to our guessing game! " + "Can you guess the number between 1 and 100?") + + # getting a random number + wanted_num = randint(1, 101) + + # getting a number of player + while True: + try: + player_num = int(input("Enter whole number: ")) + + # wrong value entery check + except ValueError: + print("You entered the wrong value, try again...") + continue + + # testing a player's number + if player_num < wanted_num: + print("Too small number...") + continue + elif player_num > wanted_num: + print("Too big number...") + continue + else: + print("!!! YOU WIN !!!") + return + + + + +print(num_guessing_game()) diff --git a/2] loto/README.md b/2] loto/README.md new file mode 100644 index 0000000..3a3f55a --- /dev/null +++ b/2] loto/README.md @@ -0,0 +1,25 @@ +## LOTTO SIMULATOR + +Simple console LOTTO application. The application gives 6 random numbers in range 1 - 49 and player is guessing them. + + +# Lauched application + +Use the "cd" command to navigate to the "2] loto" folder and enter the command: + +for LINUX + python3 main.py + +for WINDOWS + python main.py + + +# Running application + +1. The function gets the list numbers from a player and while checks: +- whether the entered text is a valid number, +- whether the user has not entered a given number before, +- if the number is in the range of 1-49. +2. The function uses randint (Random library) and gets a list of six original winning numbers in the range 1-49. +3. The function dislays list of numbers from player and list of winning numbers and then compares them. +4. The function displays how many numbers the player guessed. \ No newline at end of file diff --git a/2] loto/main.py b/2] loto/main.py index e69de29..71465a6 100644 --- a/2] loto/main.py +++ b/2] loto/main.py @@ -0,0 +1,71 @@ +from random import randint + + +def nums_of_player() -> set: + """ + The function gets a list of six original numbers + of the player. + + Funktion checks whether: + player entered integer in the range 1-49, + player has not entered a given number before. + + Funktion return a sorted list of numbers. + """ + # gettint a list of numbers + player_nums = set() + while len(player_nums)<6: + try: + num = int(input(f"Enter number No. {len(player_nums) +1}: ")) + # check a number + if num in player_nums: + print("The number has already been entered. Try again...") + elif num < 1 or num > 49: + print("The number is not in range of 1 - 49. Try again...") + else: + player_nums.add(num) + + except ValueError: + print("Value is not whole number. Try again...") + + return player_nums + + +def winning_nums() -> set: + """ + The function uses randint() and gets a list + of six original numbers in the range 1-49. + """ + draw_nums = set() + while len(draw_nums) < 6: + draw_nums.add(randint(1, 50)) + + return draw_nums + + +def main(): + """ + The function uses the "nums_of_player()" function and it gets the list + numbers from a player. Then it uses the "winning_nums()" function and + it gets list of winning numbers. + The function dislays these lists of numbers and compares them. Then it + displays how many numbers the player guessed. + """ + print(f"WELCOME TO THE PYTHON LOTTERY!\n{'=' * 30}\nWe draw " + "sixt numbers in range of 1-49. Try to guess them...") + + # getting numbers of player + player_nums = nums_of_player() + print(f"Your numbers:\n{', '.join(map(str, player_nums))}") + + # getting winning numbers + draw_nums = winning_nums() + print(f"Winning numbers:\n{', '.join(map(str, draw_nums))}") + + # evaluation of results + hits = draw_nums.intersection(player_nums) + separator = "=" * 25 + print(f"{separator}\nSUM OF GUESSED NUMBERS: {len(hits)}\n{separator}") + + +main() diff --git a/3] guessing game 2/README.md b/3] guessing game 2/README.md new file mode 100644 index 0000000..3c33183 --- /dev/null +++ b/3] guessing game 2/README.md @@ -0,0 +1,23 @@ +## Number guessing game 2 + +A simple console application that guesses a player's number in the range of 1-1000. + + +# Lauched application + +Use the "cd" command to navigate to the "3] guessing game 2" folder and enter the command: + +for LINUX + python3 main.py + +for WINDOWS + python main.py + + +# Running application + +1. The function greets a player and uses a math formula for getting most likely numbers in the range 0 - 1000. + +2. Then the function finds, if is the number correct, too hight or too low. If the quessed number is not correct, the function change numbers for calculation and repeats the process. + +3. This process is repeat only ten times, because if is it not enought, the game is not fair. diff --git a/3] guessing game 2/main.py b/3] guessing game 2/main.py index e69de29..dfc0e39 100644 --- a/3] guessing game 2/main.py +++ b/3] guessing game 2/main.py @@ -0,0 +1,62 @@ +def get_answer() -> str: + """ + A function for getting player's answer. + + A function gets the player's answer and chcek if + it is in the list of correct answers. If it is, it + return answer as string. If it is not, the function + starts again. + """ + # getting an answer + answer = input("(ALLOWED ANSWERS: too low / too high / " + "you quessed):\n") + + # answer check + correct_answers = ["too low", "too high", "you quessed"] + if answer in correct_answers: + return answer + else: + print("Wrong value, try again...") + get_answer() + + +def main(): + """ + A function for guessing a number. + + The function uses a math formula for getting most + likely numbers in the range 0 - 1000. Then the function + uses get_answer() for information, if is the number correct, + too hight or too low. + If the quessed number is not correct, the function change + numbers for calculation and repeats the process. + If the game is fair, the PC guesses the number within ten cycles. + """ + # hello area + print("Think about a number from 0 do 1000 and " + f"let me guess it!\n{'=' * 56}") + + min = 0 + max = 1000 + num_of_tries = 10 + + while num_of_tries: + # calculation of the quessed number + guess = (max-min) // 2 + min + print(f"Guessing: {guess}. Am I right?") + print(f"Pokus {num_of_tries}") + + # processing the player's response + answer = get_answer() + if answer == "too high": + max = guess + num_of_tries -= 1 + elif answer == "too low": + min = guess + num_of_tries -= 1 + elif answer == "you quessed": + print("I won!") + num_of_tries = 0 + + +main() \ No newline at end of file diff --git a/4] guessing game 3/README.md b/4] guessing game 3/README.md new file mode 100644 index 0000000..85da2e5 --- /dev/null +++ b/4] guessing game 3/README.md @@ -0,0 +1,25 @@ +## Number guessing game 2 + +Flasková aplikace, ve které PC hádá číslo v rozsahu 0-1000. + + +# Lauched application + +Use the "cd" command to navigate to the "4] guessing game 3" folder and enter the command: + +for LINUX + python3 main.py + +for WINDOWS + python main.py + + +# Running application + +1. Aplikace uvítá hráče a zeptá se ho, jestli chce hrát. + +2. Po potvrzení se zobrazí hrací stránka s tipem čísla od počítače a hráč je vyzván, aby řekl, jestli je číslo moc vysoké nebo nízké nebo jestli je to správné číslo. + +3. Po kliknutí na tlačítko "too low" nebo "too hide" se tip čísla přepočítá a znovu se opakuje bod dva. + +4. Pokud je tipované číslo správné, hráč klikne na tlačítko "you win". PC poděkuje za hru a dá hráči možnost zahrát si zvovu. diff --git a/4] guessing game 3/main.py b/4] guessing game 3/main.py index e69de29..4f17bb6 100644 --- a/4] guessing game 3/main.py +++ b/4] guessing game 3/main.py @@ -0,0 +1,60 @@ +from flask import Flask, request, render_template + +app = Flask(__name__) + + +def calculation(mnimum=int, mximum=int) -> int: + """ + Fukce se základním vzorcem pro výpočet + tipu čísla. + """ + return (mximum-mnimum) // 2 + mnimum + + +@app.route("/", methods=["GET", "POST"]) +def game(): + """ + Aplikace pro hádání čísla hráče v rozsahu 0-1000. + Funkce získá vstupní hodnoty mn a mx, díky kterým + vypočítává nejpravděpodobnější číslo. Hodnoty jsou na + začátku nastavené defaltně a v průběhu hry se upravují + podle reakce uživatele dokud aplikace nedopočítá správné + číslo. + """ + # zobrazení úvodní stránky hry + if request.method == "GET": + return render_template("index.html") + + # zobrazení hrací stránky + elif request.method == "POST": + # získání hodnot mn/mx/guess + mn = int(request.form.get("mn", default=0)) + mx = int(request.form.get("mx", default=1000)) + guess = 0 + + # úprava hodnot podle vstupu uživatele + answer = request.form.get("button") + if answer == "low": + mn = int(request.form.get("guess")) + print(mn, mx) + guess = calculation(mn, mx) + return render_template("guessing.html", guess=guess, mn=mn, mx=mx) + + elif answer == "high": + mx = int(request.form.get("guess")) + print(mn, mx) + guess = calculation(mn, mx) + return render_template("guessing.html", guess=guess, mn=mn, mx=mx) + + # zobrazení výherní stránky + elif answer == "win": + return render_template("i_win.html") + + # první zobrazení hrací stránky (bez vstupu uživatele) + else: + guess = calculation(mn, mx) + return render_template("guessing.html", guess=guess, mn=mn, mx=mx) + + +if __name__ == "__main__": + app.run(debug=True) \ No newline at end of file diff --git a/4] guessing game 3/templates/guessing.html b/4] guessing game 3/templates/guessing.html new file mode 100644 index 0000000..3bc97a6 --- /dev/null +++ b/4] guessing game 3/templates/guessing.html @@ -0,0 +1,38 @@ + + +
+ + +Thank you for a game! Do you want to play again? +
+ + +RULES: Think about a number in the range
+ 0 - 1000.
Then say me, if my guess is right, too
+ high or too low.