diff --git a/RockPaperScissors.py b/RockPaperScissors.py index 2bb0ddd1..825f1f15 100644 --- a/RockPaperScissors.py +++ b/RockPaperScissors.py @@ -1,16 +1,52 @@ + import random +from termcolor import colored + +try: + + def rock_paper_scissors(): + print("Welcome to Rock, Paper, Scissors Game!") + + score = 0 + + while True: + + choice = ["rock", "paper", "scissor"] + player_choice = input("Enter your choice (rock, paper and scissor) or 'Q' to quite: ").lower().strip() + + + if player_choice.lower().strip() == "q": + print("Thanks for palying.") + break + + + if player_choice not in choice: + print("Invalid input!") + continue + + computer_choice = random.choice(choice) + print(f"Computer choose {computer_choice}") + + if player_choice == computer_choice: + print(colored("It's a tie!", "blue")) + elif (player_choice == "rock" and computer_choice == "scissor") or \ + (player_choice == "paper" and computer_choice == "rock") or \ + (player_choice == "scissor" and computer_choice == "paper"): + score += 1 + print(colored("You win!", "green")) + + else: + print(colored("You lose!", "red")) + + print(colored(f"Your score: {score}", "light_yellow")) + +except: + print("") + +(rock_paper_scissors()) + + + + -randomInteger=random.randint(0,2) -userInput=int(input("What do you choose ? Type 0 for Rock,1 for Paper or 2 for Scissors\n")) -if(userInput!=0 and userInput!=1 and userInput!=2): - print("Wrong choise") - exit() - -if(userInput==randomInteger): - print("DRAW!!!") -elif (userInput==randomInteger-1 or (userInput==2 and randomInteger==0)): - print("Computer Won") -elif (randomInteger==userInput-1 or (randomInteger==2 and userInput==0)): - print("YOU WON!!!") -print(userInput) -print(f"computer choose {randomInteger}") \ No newline at end of file + \ No newline at end of file diff --git a/calculator.py b/calculator.py index 6f0f018c..a3243f5d 100644 --- a/calculator.py +++ b/calculator.py @@ -1,59 +1,63 @@ -num1=int(input("eneter a digit")) -num2=int(input("eneter a another digit")) -# defination for operators - -#addition -def add(num1, num2): - return num1+num2 -#substraction -def subtract(num1, num2): - return num1-num2 -#multiply -def multiply(num1, num2): - return num1*num2 -#division -def divide(num1, num2): - return num1/num2 - -#command for operation -print("choose operation") -print("press 1 for add") -print("press 2 for subs") -print("press 3 for multiply") -print("press 4 for devision") - - - - +print("Welcome to basic python calculator", "\n") while True: - # take input from the user - choice = input("Enter choice(1/2/3/4): ") - - if choice in ('1', '2', '3', '4'): - - if choice == '1': - print(num1, "+", num2, "=", add(num1, num2)) - - - - elif choice == '2': - print(num1, "-", num2, "=", subtract(num1, num2)) - - elif choice == '3': - print(num1, "*", num2, "=", multiply(num1, num2)) - - - - - - elif choice == '4': - print(num1, "/", num2, "=", divide(num1, num2)) - # check if user wants another calculation - # break the while loop if answer is no - next_calculation = input("Let's do next calculation? (yes/no): ") - if next_calculation == "no": + while True: + try: + num1 = int(input("Enter first digit: ")) break + except ValueError: + print("Invalid input! Please enter an integer.") - else: - print("Invalid Input") + while True: + try: + num2 = int(input("Enter second digit: ")) + break + except ValueError: + print("Invalid input! Please enter an integer.") + + # Define operations + def add(num1, num2): + return num1 + num2 + + def subtract(num1, num2): + return num1 - num2 + + def multiply(num1, num2): + return num1 * num2 + + def divide(num1, num2): + if num2 == 0: + return "Error! Division by zero." + return num1 / num2 + + # Display operation choices + print("\nChoose an operation: ") + print("Press 1 for addition (+)") + print("Press 2 for subtraction (-)") + print("Press 3 for multiplication (*)") + print("Press 4 for division (/)") + + while True: + try: + choice = int(input("Enter your choice (1/2/3/4): ")) + + if choice == 1: + print(num1, "+", num2, "=", add(num1, num2)) + elif choice == 2: + print(num1, "-", num2, "=", subtract(num1, num2)) + elif choice == 3: + print(num1, "*", num2, "=", multiply(num1, num2)) + elif choice == 4: + print(num1, "/", num2, "=", divide(num1, num2)) + else: + print("Invalid input! Please choose between (1-4)") + continue # Ask again if the input is not 1-4 + break # Exit choice loop after a valid selection + except ValueError: + print("Invalid input! Please enter a number between 1 and 4.") + + # Ask if the user wants to continue + next_calculation = input("Do you want to continue? (yes/no): ").strip().lower() + if next_calculation == "no": + print("Goodbye!") + break