diff --git a/1] guessing game/main.py b/1] guessing game/main.py index e69de29..acb3474 100644 --- a/1] guessing game/main.py +++ b/1] guessing game/main.py @@ -0,0 +1,24 @@ +from random import randint + + +def guessing_game(): + number = int(randint(1, 100)) + + while True: + guess = input("Guess the number: ") + + try: + guess = int(guess) + if guess < number: + print("Too small!") + elif guess > number: + print("Too big!") + else: + print("You win!") + break + except ValueError: + print("It's not a number!") + + +if __name__ == '__main__': + guessing_game() diff --git a/2] loto/main.py b/2] loto/main.py index e69de29..3ed311e 100644 --- a/2] loto/main.py +++ b/2] loto/main.py @@ -0,0 +1,59 @@ +from random import shuffle + + +def user_guess(): + user_guess = [] + order = 1 + while len(user_guess) != 6: + try: + num = int(input(f"Enter {order}. number: ")) + if num in user_guess: + print("You entered the same number!") + # user_guess = sorted(user_guess) + print(f"Your numbers are currently: {tuple(sorted(user_guess))}") + elif num < 1 or num > 49: + print("Enter a number between 1-49") + else: + user_guess.append(num) + order += 1 + except ValueError: + print("Must entered a number!") + return user_guess + + +def lotto_numbers(): + numbers = [i for i in range(1, 50)] + shuffle(numbers) + numbers = numbers[:6] + return numbers + + +def format_numbers(numbers): + print(", ".join(str(i) for i in sorted(numbers))) + + +def lotto(): + matched_numbers = [] + count = 0 + user_numbers = user_guess() + lotto_num = lotto_numbers() + print("\nYour numbers:") + format_numbers(user_numbers) + print("Lotto numbers: ") + format_numbers(lotto_num) + for i in user_numbers: + if i in lotto_num: + count += 1 + matched_numbers.append(i) + + if count >= 1: + print(f"Matched numbers: ") + format_numbers(matched_numbers) + if count == 1: + print(f"You guessed {count} number!") + else: + print(f"You guessed {count} numbers!") + + +if __name__ == '__main__': + lotto() diff --git a/3] guessing game 2/main.py b/3] guessing game 2/main.py index e69de29..67adb1c 100644 --- a/3] guessing game 2/main.py +++ b/3] guessing game 2/main.py @@ -0,0 +1,33 @@ +def answer(): + posibble_answer = ["too small", "too big", "you win"] + while True: + user_answer = input("Is this a correct guess?: ") + user_answer.lower() + if user_answer in posibble_answer: + break + else: + print("You can only write 'too big', too small', 'you win')") + return user_answer + + +def guessing_game(): + print("Think about a number from 0 to 100 and let me guess it!") + print("You can only write 'too big', too small', 'you win')") + min_num = 0 + max_num = 1000 + user_answer = "" + while True: + guess = int((max_num - min_num) / 2) + min_num + print(f"Guessing: {guess}") + user_answer = answer() + if user_answer == "too big": + max_num = guess + elif user_answer == "too small": + min_num = guess + elif user_answer == "you win": + print("I won!") + break + + +if __name__ == "__main__": + guessing_game() diff --git a/4] guessing game 3/main.py b/4] guessing game 3/main.py index e69de29..d482070 100644 --- a/4] guessing game 3/main.py +++ b/4] guessing game 3/main.py @@ -0,0 +1,33 @@ +from flask import Flask, request, render_template + +app = Flask(__name__) + + +@app.route("/") +def guessing_game(): + return render_template("index.html", min=0, max=1000, guess=500, condition=False) + + +@app.route("/", methods=["POST"]) +def guessing_game_post(): + min_number = int(request.form["min"]) + max_number = int(request.form["max"]) + user_input = request.form["user_input"] + guess = int(request.form["guess"]) + condition = False + + if user_input == "Too small": + min_number = guess + elif user_input == "Too big": + max_number = guess + elif user_input == "You win": + condition = True + return render_template("index.html", guess=guess, condition=condition) + + guess = int(((max_number - min_number) / 2) + min_number) + + return render_template("index.html", min=min_number, max=max_number, guess=guess, condition=condition) + + +if __name__ == "__main__": + app.run(debug=True) diff --git a/4] guessing game 3/templates/index.html b/4] guessing game 3/templates/index.html new file mode 100644 index 0000000..259ed56 --- /dev/null +++ b/4] guessing game 3/templates/index.html @@ -0,0 +1,28 @@ + + + + + Guessing game + + + {% if not condition %} +

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

+ +

My guess: {{ guess }}

+ +
+ + + + + + +
+ {% else %} +

I Won! Your number was {{ guess }}

+ {% endif %} + + {% print(min_num) %} + + + \ No newline at end of file diff --git a/5] dice/complicated_solution.py b/5] dice/complicated_solution.py new file mode 100644 index 0000000..b1625d8 --- /dev/null +++ b/5] dice/complicated_solution.py @@ -0,0 +1,44 @@ +from random import randint + +dice = ("D3", "D4", "D6", "D8", "D10", "D12", "D20", "D100") + + +def format_dice(dice): + dice_split = dice.split("D") + if len(dice_split) != 2: + print("Wrong format of dice") + number_of_rolls, type_and_modifier = dice_split + + if not number_of_rolls: + number_of_rolls = 1 + + if "+" in type_and_modifier: + x = type_and_modifier.split("+") + type_of_dice, modifier = map(int, x) # type_of_dice, modifier = x + print(f"+: {number_of_rolls, type_of_dice, modifier}") + print(f"Type +: number_of_rolls", type(type_of_dice)) + print(f"Type +: type_of_dice", type(modifier)) + print(f"Type +: modifier", type(modifier)) + return number_of_rolls, type_of_dice, modifier + elif "-" in type_and_modifier: + x = type_and_modifier.split("-") + type_of_dice, modifier = map(int, x) + print(f"-: {number_of_rolls, type_of_dice, modifier}") + print(f"Type -: number_of_rolls", type(type_of_dice)) + print(f"Type -: type_of_dice", type(modifier)) + print(f"Type -: modifier", type(modifier)) + return number_of_rolls, type_of_dice, -modifier + + print(f"Type d6 number:", type(number_of_rolls)) + print(f"Type d6 type:", type(type_and_modifier)) + + return number_of_rolls, type_and_modifier, None + + +def roll_the_dice(): + pass + + +if __name__ == "__main__": + roll = input("Enter a roll the dice: ").upper() + print(format_dice(roll)) diff --git a/5] dice/main.py b/5] dice/main.py index e69de29..5d804ed 100644 --- a/5] dice/main.py +++ b/5] dice/main.py @@ -0,0 +1,39 @@ +from random import randint + +dice = ("D3", "D4", "D6", "D8", "D10", "D12", "D20", "D100") + + +def roll_the_dice(input_dice): + for cube in dice: + if cube in input_dice: + try: + number_of_rolls, modifier = input_dice.split(cube) + except ValueError: + return "Wrong Dice" + type_of_dice = cube[1:] + break + else: + return "Wrong dice" + + if number_of_rolls == "": + number_of_rolls = 1 + + if modifier == "": + modifier = 0 + + number_of_rolls, type_of_dice, modifier = map(int, [number_of_rolls, type_of_dice, modifier]) + + # dice_sum = 0 + # for i in range(number_of_rolls): + # dice_sum += randint(1, type_of_dice) + # + # calculation1 = dice_sum + modifier + # print(f"Calculation1: {calculation1}") + + calculation = sum([randint(1, type_of_dice) for i in range(number_of_rolls)]) + modifier + + return f"The Result of the dice roll {input_dice} is {calculation}" + + +if __name__ == "__main__": + print(roll_the_dice("2D6+4"))