diff --git a/1] guessing game/main.py b/1] guessing game/main.py index e69de29..336b395 100644 --- a/1] guessing game/main.py +++ b/1] guessing game/main.py @@ -0,0 +1,29 @@ +from random import randint + + +def guess_num(): + """ + guess_num function take number from user input + then compare it with chosen random number 1-100 + and react in proper way for tip <, > or == + """ + x = input("Guess the number:") + try: + x = int(x) + if x < number: + print("Too small!") + return guess_num() + elif x > number: + print("Too big!") + return guess_num() + elif x == number: + print("You win!") + except ValueError: + print("It's not a number!") + return guess_num() + + +number = randint(1, 100) + +if __name__ == '__main__': + guess_num() diff --git a/2] loto/main.py b/2] loto/main.py index e69de29..ab05d72 100644 --- a/2] loto/main.py +++ b/2] loto/main.py @@ -0,0 +1,52 @@ +from random import choice + + +def player_tip(): + """ + player_tip function ask player for 6 numbers from 1 to 49 + with validation for range, repetition and value + """ + tip_list = [] + while len(tip_list) < 6: + try: + x = (int(input("Choose number from 1 to 49:\n"))) + if x not in tip_list and x in range(1, 50): + tip_list.append(x) + else: + print("Please choose different number") + except ValueError: + print("Invalid number, try again") + return sorted(tip_list) + + +def computer_tip(): + """ + computer_tip function draw 6 different numbers from 1 to 49 + """ + range_list = list(range(1, 50)) + c_tip_list = [] + while len(c_tip_list) < 6: + y = choice(range_list) + c_tip_list.append(y) + range_list.remove(y) + return sorted(c_tip_list) + + +def lotto(): + """ + lotto collects player_tip and computer_tip results and count + similar tips + """ + player_list = player_tip() + computer_list = computer_tip() + matched = 0 + for tip in player_list: + if tip in computer_list: + matched += 1 + print(f"Player tips :\n {player_list}") + print(f"Computer tips :\n {computer_list}") + print(f"You guessed {matched} number/s") + + +if __name__ == '__main__': + lotto() diff --git a/3] guessing game 2/main.py b/3] guessing game 2/main.py index e69de29..50cbe8f 100644 --- a/3] guessing game 2/main.py +++ b/3] guessing game 2/main.py @@ -0,0 +1,32 @@ +def guessing_from_player(): + """ + guessing_from_player program ask player to think number from 0 to 1000 + then asking for value and react if too low, too high, or you guessed entered + if not guessed in 10 round, player is cheating + :return: + """ + print("""Think about a number from 0 to 1000, and let me guess it! + Press any key when you are ready""") + input() + round = 0 + min = 0 + max = 1000 + while round < 10: + guess = int((max - min) / 2) + min + print(f"Guessing: {guess}") + answer = input("Enter too low, too high or you guessed:\n").lower() + if answer == "you guessed": + return print("I won!") + elif answer == "too high": + max = guess + round += 1 + elif answer == "too low": + min = guess + round += 1 + else: + print("Please enter only too low, too high or you guessed") + print("Don't cheat!") + + +if __name__ == '__main__': + guessing_from_player() diff --git a/4] guessing game 3/main.py b/4] guessing game 3/main.py index e69de29..9774a9c 100644 --- a/4] guessing game 3/main.py +++ b/4] guessing game 3/main.py @@ -0,0 +1,48 @@ +from flask import Flask, request, render_template +app = Flask(__name__) + +def calc(): + """ + calc function takes min and max from web form, calculate and return guess + :return: + """ + min = int(request.form['min']) + max = int(request.form['max']) + guess = ((max - min) // 2) + min + return guess +@app.route("/", methods=['GET', 'POST']) +def guessing_from_player(): + """ + guessing_from_player program /w flask ask player to think a number from 0 to 1000 + reacting to POST returned value and calculate new guess + :return: + """ + if request.method == "POST": + answer = request.form['guess_button'] + round = int(request.form['round']) + if round == 10: + return render_template("cheater.html") + if answer == "you guessed": + return render_template("win.html") + elif answer == "too high": + max = calc() + min = int(request.form['min']) + guess = ((max - min) // 2) + min + round += 1 + return render_template("index.html", guess=guess, min=min, max=max, round=round) + elif answer == "too low": + min = calc() + max = int(request.form['max']) + guess = ((max - min) // 2) + min + round += 1 + return render_template("index.html", guess=guess, min=min, max=max, round=round) + else: + min = 0 + max = 1000 + round = 1 + guess = ((max - min) // 2) + min + return render_template("index.html", guess=guess, min=min, max=max, round=round) + + +if __name__ == '__main__': + app.run(debug=True) diff --git a/4] guessing game 3/static/cheater.jpg b/4] guessing game 3/static/cheater.jpg new file mode 100644 index 0000000..b25194d Binary files /dev/null and b/4] guessing game 3/static/cheater.jpg differ diff --git a/4] guessing game 3/static/win.jpg b/4] guessing game 3/static/win.jpg new file mode 100644 index 0000000..b04ea9f Binary files /dev/null and b/4] guessing game 3/static/win.jpg differ diff --git a/4] guessing game 3/templates/cheater.html b/4] guessing game 3/templates/cheater.html new file mode 100644 index 0000000..f46a1eb --- /dev/null +++ b/4] guessing game 3/templates/cheater.html @@ -0,0 +1,11 @@ + + + + + Cheater + + +

You are cheater!

+You really disappointed me... + + \ No newline at end of file diff --git a/4] guessing game 3/templates/index.html b/4] guessing game 3/templates/index.html new file mode 100644 index 0000000..6276a86 --- /dev/null +++ b/4] guessing game 3/templates/index.html @@ -0,0 +1,20 @@ + + + + + Guessing game + + +

Think about a number from 0 to 1000, and let me guess it!

+

My tip is : {{guess}}

+
+ + + + + + +
+ + + \ No newline at end of file diff --git a/4] guessing game 3/templates/win.html b/4] guessing game 3/templates/win.html new file mode 100644 index 0000000..2c9382c --- /dev/null +++ b/4] guessing game 3/templates/win.html @@ -0,0 +1,14 @@ + + + + + Computers epic win + + +

I WON

+

I WON

+

I WON

+I won + + + \ No newline at end of file diff --git a/5] 2001/main.py b/5] 2001/main.py index e69de29..c7922da 100644 --- a/5] 2001/main.py +++ b/5] 2001/main.py @@ -0,0 +1,47 @@ +from random import randint + + +def game(): + """ + game is 2001 game with throwing 2 D6 dices, adding throw value to score. + From second turn if roll is 7 then divide score by 7, if 11, multiply score by 11. + First player who hit or exceeded 2001 wins + :return: + """ + player1_points = 0 + player2_points = 0 + turn = 0 + while player1_points < 2001 and player2_points < 2001: + input("Press enter to throw") + turn += 1 + player1 = d6_throw() + player2 = d6_throw() + if turn >= 2 and player1 == 7: + player1_points = player1_points // 7 + elif turn >= 2 and player1 == 11: + player1_points = player1_points * 11 + else: + player1_points += player1 + + if turn >= 2 and player2 == 7: + player2_points = player2_points // 7 + elif turn >= 2 and player2 == 11: + player2_points = player1_points * 11 + else: + player2_points += player2 + print(f"Player points : {player1_points}") + print(f"Computer points : {player2_points}") + if player1_points >= 2001: + return("Player WIN!") + elif player2_points >= 2001: + return ("Computer WIN!") + + +def d6_throw(): + """ + d6_throw represents 2 throws of D6 dices + :return: + """ + return randint(1, 6) + randint(1, 6) + +print(game()) \ No newline at end of file diff --git a/5] 2001/mod1.py b/5] 2001/mod1.py new file mode 100644 index 0000000..06d7141 --- /dev/null +++ b/5] 2001/mod1.py @@ -0,0 +1,75 @@ +from random import randint, choice + +dices = ("D3", "D4", "D6", "D8", "D10", "D12", "D20", "D100") + + +def dice_throw(): + """ + dice_throw function takes input from user, confirming existing dice. + After that simulate throw of dice. 2 choices of dice and throws in total. + :return: + """ + result = 0 + throw = 0 + while throw < 2: + for take in range(2): + command = input("Enter dice code D3, D4, D6, D8, D10, D12, D20 or D100:\n") + if command not in dices: + print("Wrong input") + else: + dice = int(command.removeprefix("D")) + result += randint(1, dice) + throw += 1 + return result + + +def comp_dice_throw(): + """ + comp_dice_throw simulates random choice of 2 dices and throw + :return: + """ + result = 0 + for take in range(2): + command = choice(dices) + dice = int(command.removeprefix("D")) + result += randint(1, dice) + return result + + +def game(): + """ + game is 2001 game with throwing 2 D6 dices, adding throw value to score. + From second turn if roll is 7 then divide score by 7, if 11, multiply score by 11. + First player who hit or exceeded 2001 wins + :return: + """ + player1_points = 0 + player2_points = 0 + turn = 0 + while player1_points < 2001 and player2_points < 2001: + turn += 1 + player1 = dice_throw() + player2 = comp_dice_throw() + if turn >= 2 and player1 == 7: + player1_points = player1_points // 7 + elif turn >= 2 and player1 == 11: + player1_points = player1_points * 11 + else: + player1_points += player1 + + if turn >= 2 and player2 == 7: + player2_points = player2_points // 7 + elif turn >= 2 and player2 == 11: + player2_points = player1_points * 11 + else: + player2_points += player2 + print(f"Player points : {player1_points}") + print(f"Computer points : {player2_points}") + if player1_points >= 2001: + return "Player WIN!" + elif player2_points >= 2001: + return "Computer WIN!" + + +if __name__ == '__main__': + print(game()) diff --git a/5] dice/main.py b/5] dice/main.py index e69de29..a8c14b0 100644 --- a/5] dice/main.py +++ b/5] dice/main.py @@ -0,0 +1,39 @@ +from random import randint +import re + +dices = ("3", "4", "6", "8", "10", "12", "20", "100") +dice_pattern = re.compile(r"^(\d*)D(\d+)([+-]\d+)?$") + + +def dice_throw(): + """ + dice_throw function takes input from user, confirming dice_pattern. + Divide entry to multiply, modifier and dice, then calculate random throws and return value. + :return: + """ + command = input("Enter dice code:\n") + match = dice_pattern.search(command) + if not match: + return "Wrong input" + + multiply, dice, modifier = match.groups() + if dice not in dices: + return "Wrong input" + + if multiply: + multiply = int(multiply) + else: + multiply = 1 + + if modifier: + modifier = int(modifier) + else: + modifier = 0 + + dice = int(dice) + + return sum([randint(1, dice) for _ in range(multiply)]) + modifier + + +if __name__ == '__main__': + print(dice_throw()) diff --git a/test2.py b/test2.py new file mode 100644 index 0000000..3c54b0a --- /dev/null +++ b/test2.py @@ -0,0 +1 @@ +print("Hi there, its working now?")