-
Notifications
You must be signed in to change notification settings - Fork 16
Workshop 03022024 #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
be1ffea
8fa64c0
1020070
44de903
b651e2d
89376fc
ce5e942
9874229
bda1fa7
433d0df
97bcbb6
fae5946
fce33c5
746f445
89adde1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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". |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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()) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to delas rekurzivne jo? to je teda fikany :) Potencialne se ti ale muze stat, ze na to crashne cely program kdyz bude nekdo dostatecne dlouho zadavat chybne hodnoty, protoze se bude kupit kupit rekurzivni volani funkci na sebe a to ma nejkay limit
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Jj, učili mě, že se máme vyvarovat tomu, abychom psali stejný kód více krát, tak mi to přišlo nejjednodušší. Mám to přepsat? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no to bys mela. anebo musis osetrit ze to nevyhodi eror. ale to nemusi opakovat. Staci to misto do rekurze dat do loopu preci |
||
|
|
||
|
|
||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pracovat s numer of tries a mit to jako kontrolni promenou v loopu je trochu divny - proc to delas takhle?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Přijde mi to nejkratší podmínka pro while, aby vracela True, pokud má hráč ještě pokusy a False, pokud je už vyčerpal. V téhle části jsem zvažovala, jestli nepoužít break, ale použití num_of_tries = 0 mi přišlo pro ostatní čtenáře mého kódu čitelnější, že se pořád držím stejného principu, že jestli while loop jede závisí na téhle proměnné. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ne, to neni ze ma pokusy nebo vycerpal ten algortmus sam o sobe zajisti, ze nemuze prekrocit 10 pokusu na 1000 protoze bezi v logaritmicke komlexite a log(1000) = 10. ty tam vubec tohle nemas kontrolovat |
||
|
|
||
|
|
||
| main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Guessing GAME</title> | ||
| <style> | ||
| button { | ||
| margin: 10px auto; | ||
| min-width: 150px; | ||
| min-height: 35px; | ||
| font-size: 17px; | ||
| } | ||
| .box { | ||
| text-align: center; | ||
| margin: 50px auto; | ||
| } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <div class="box"> | ||
| <h1>GUESSING GAME</h1> | ||
|
|
||
| <h2>I guess {{ guess }}, it is right?</h2> | ||
|
|
||
| <form action="/" method="POST"> | ||
| <button type="submit" name="button" value="low">TOO LOW</button> | ||
| <button type="submit" name="button" value="high">TOO HIGH</button> | ||
| <button type="submit" name="button" value="win">YOU WIN</button> | ||
| <input type="hidden" name="guess" value="{{ guess }}"> | ||
| <input type="hidden" name="mn" value="{{ mn }}"> | ||
| <input type="hidden" name="mx" value="{{ mx }}"> | ||
| </form> | ||
|
|
||
| </form> | ||
| </div> | ||
| </body> | ||
| </html> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ten try je zbutecne velky, idealne je obalit jen tu cast kodu kde cekas tu vyjimku
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opraveno, jak dodělám všechny opravy, postnu.