Skip to content

Added input validation for integer inputs #538

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 50 additions & 14 deletions RockPaperScissors.py
Original file line number Diff line number Diff line change
@@ -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}")

114 changes: 59 additions & 55 deletions calculator.py
Original file line number Diff line number Diff line change
@@ -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