-
Notifications
You must be signed in to change notification settings - Fork 16
workshop #6
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?
workshop #6
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| from random import randint | ||
| number = randint(1, 100) | ||
| win = False | ||
| while win == False: | ||
| #system generates a number in range 1-100 and user tries to guess it | ||
| #system provides clues Too small/too big and validates whether input is a number | ||
| try: | ||
| user_input = int(input("Guess a number between 1 and 100: ")) | ||
| if user_input < number: | ||
| print("Too small") | ||
| elif user_input > number: | ||
| print("Too big") | ||
| else: | ||
| print("You win!") | ||
| win = True | ||
| except ValueError: | ||
| print("It's not a number!") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| from random import randint | ||
|
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. zase ten kod by mel byt zalbaleny do funcki |
||
|
|
||
| user_input=[] | ||
| drawn_numbers=[] | ||
| matched=0 | ||
| #system collects 6 different numbers in given range and sorts them | ||
| for x in range(6): | ||
| while True: | ||
|
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. tady delas dvijity loop proc? to je zbytecne |
||
| try: | ||
| number = int(input(f"Input {x+1}. number in range 1-49: ")) | ||
| if number not in user_input and number in range(1,49): | ||
| user_input.append(number) | ||
| break | ||
| else: | ||
| print("Every number can be picked just once and must be in given range!") | ||
| except ValueError: | ||
| print("Input is not a number") | ||
| user_input.sort() | ||
| print(user_input) | ||
|
|
||
| #system draws 6 random numbers and sorts them | ||
| for x in range(6): | ||
| while True: | ||
|
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. taky dvojity loop uplne zbytecne. jdi na to vic primocare |
||
| number = randint(1,49) | ||
| if number not in drawn_numbers: | ||
| drawn_numbers.append(number) | ||
| break | ||
| drawn_numbers.sort() | ||
| print(drawn_numbers) | ||
|
|
||
| #system checks whether there are some matches in user input and system draws | ||
| for x in range(6): | ||
| if user_input[x] in drawn_numbers: | ||
| matched +=1 | ||
| print(f'You matched {matched} number(s)') | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| print("Think about a number from 0 to 1000 and let me guess it!") | ||
|
|
||
| answer = "" | ||
| min = 0 | ||
| max = 1000 | ||
| count = 0 | ||
|
|
||
| #the cycle continues till the guess is correct | ||
| while answer != "3": | ||
|
|
||
| guess = int((max - min) / 2) + min | ||
|
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. flor div // je lepsi |
||
|
|
||
| #If the count of guesses reaches 10, user must be cheating, therefore system calls him liar | ||
| #Otherwise system cointinues guessing | ||
| if count == 10: | ||
|
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. toto neni vubec potreba kontrolovat |
||
| print("Liar liar pants on fire!") | ||
| break | ||
| else: | ||
| print(f"Guessing: {guess}") | ||
| count += 1 | ||
|
|
||
| #user inputs a number to give a clue to the system | ||
| answer = input("Your verdict 1 - Too small, 2 - Too big, 3 - You win => ") | ||
|
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. neni osetrena vyjimka kdyz nekdo zada neco jineho nez 3 nebo 2. Vlastne to skace vzdy do else vetve kdyz clovek zada neco jineho, takze tahle logika neni spravne |
||
|
|
||
| if answer == "3": | ||
| print("I won! Thanks for game") | ||
| elif answer == "2": | ||
| max = guess | ||
| else: | ||
| min = guess | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| from flask import Flask, render_template, request | ||
|
|
||
| app = Flask(__name__, template_folder='templates') | ||
|
|
||
| @app.route('/', methods=['GET', 'POST']) | ||
| def guess_number(): | ||
| min_num = 0 | ||
| max_num = 1000 | ||
| count = 0 | ||
|
|
||
| #Flask post method that handles flow of values | ||
| if request.method == 'POST': | ||
| min_num = int(request.form.get('min')) | ||
| max_num = int(request.form.get('max')) | ||
| count = int(request.form.get('count')) | ||
| answer = request.form['answer'] | ||
| count += 1 | ||
|
|
||
| # If the count of guesses reaches 10, user must be cheating, therefore system calls him liar | ||
| if count == 10: | ||
| return "Liar liar pants on fire!" | ||
|
|
||
| if answer == "3": | ||
| return "I won! Thanks for game" | ||
| elif answer == "2": | ||
| max_num = int((max_num - min_num) / 2) + min_num | ||
| else: | ||
| min_num = int((max_num - min_num) / 2) + min_num | ||
| #counting number to guess | ||
| guess = int((max_num - min_num) / 2) + min_num | ||
|
|
||
| #template and variable handling | ||
| return render_template('guess.html', min=min_num, max=max_num, count=count, guess=guess) | ||
|
|
||
| if __name__ == '__main__': | ||
| app.run(debug=True) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>Number Guessing Game</title> | ||
| </head> | ||
| <body> | ||
| <h1>Number Guessing Game</h1> | ||
| <p>Think about a number from 0 to 1000 and let me guess it!</p> | ||
| <p>Guessing: {{ guess }}</p> | ||
| <form method="POST"> | ||
| <input type="hidden" name="min" value="{{ min }}"> | ||
| <input type="hidden" name="max" value="{{ max }}"> | ||
| <input type="hidden" name="count" value="{{ count }}"> | ||
| <input type="hidden" name="answer" value="2"> | ||
| <input type="submit" value="Too big"> | ||
| </form> | ||
| <form method="POST"> | ||
| <input type="hidden" name="min" value="{{ min }}"> | ||
| <input type="hidden" name="max" value="{{ max }}"> | ||
| <input type="hidden" name="count" value="{{ count }}"> | ||
| <input type="hidden" name="answer" value="1"> | ||
| <input type="submit" value="Too small"> | ||
| </form> | ||
| <form method="POST"> | ||
| <input type="hidden" name="min" value="{{ min }}"> | ||
| <input type="hidden" name="max" value="{{ max }}"> | ||
| <input type="hidden" name="count" value="{{ count }}"> | ||
| <input type="hidden" name="answer" value="3"> | ||
| <input type="submit" value="You win"> | ||
| </form> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import re | ||
| import random | ||
| #using regex and random to simulate the dice throw | ||
| def roll_dice(dice_code): | ||
| pattern = r'(\d*)D(\d+)([-+]\d+)?' | ||
|
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. tenhle pattern neni spravne. umozni to napsat treba D7 coz je zakazno |
||
| match = re.match(pattern, dice_code) | ||
|
|
||
| try: | ||
| num_rolls = int(match.group(1)) if match.group(1) else 1 | ||
| dice_type = int(match.group(2)) | ||
| modifier = int(match.group(3)) if match.group(3) else 0 | ||
| result = sum(random.randint(1, dice_type) for _ in range(num_rolls)) + modifier | ||
|
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. wau!! tohle je celkem optimalni resesni, skoda ze nemas dobre ten pattern. |
||
| return result | ||
| except AttributeError: | ||
| return "Invalid dice" | ||
|
|
||
|
|
||
| print(roll_dice("2D10+10")) | ||
| print(roll_dice("D6")) | ||
| print(roll_dice("2D3")) | ||
| print(roll_dice("D12-1")) | ||
| print(roll_dice("XD1")) | ||
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 kod by mel byt zabaleny do funcki.