World, meet PyCalc! A Calculator that does the following: Addition, Subtraction, Multiplication, Division, Percentage, Finding Circumfrence, Finding Area, and Finding the Area of a Circle! Give it a go!
#Another python script that i thought wasnt going to exist yet here we are and here we be as I died.
#This is a simple calculator. It performs the following functions: adding, subtracting, multiplying, dividing,
import math as m
n = []
def main():
print("Are you adding (add), subtracting (sub), multiplying (mul), or dividing (div) today? Type 'oth' for more options.")
inp = input()
if inp.lower() == "add":
return add()
elif inp.lower() == "sub":
return sub()
elif inp.lower() == "mul":
return mul()
elif inp.lower() == "div":
return div()
elif inp.lower() == "oth":
print("Convert decimal to percent (per)\nFind cricumfrence (cir)\nFind area of a 2D shape(ar)\nFind area of a circle (car)")
inp = input()
if inp.lower() == "per":
return per()
elif inp.lower() == "cir":
return cir()
elif inp.lower() == "ar":
return ar()
elif inp.lower() == "car":
return car()
else:
return main()
else:
return main()
#Adding function
def add():
n.clear()
inp = input("Input a number: ")
n.append(inp)
inp = input("Input another number: ")
n.append(inp)
summ = float(n[0]) + float(n[1])
print(summ)
return main()
#Subtracting function
def sub():
n.clear()
inp = input("Input a number: ")
n.append(inp)
inp = input("Input another number: ")
n.append(inp)
dif = float(n[0]) - float(n[1])
print(dif)
return main()
#Multiplying function
def mul():
n.clear()
inp = input("Input a number: ")
n.append(inp)
inp = input("Input another number: ")
n.append(inp)
print("Math Homework Complete")
product = float(n[0]) * float(n[1])
print(product)
return main()
#Dividing function
def div():
n.clear()
inp = input("Input a number: ")
n.append(inp)
inp = input("Input another number: ")
n.append(inp)
print("Math Homework Complete")
product = float(n[0]) / float(n[1])
print(product)
return main()
#Percentage function
def per():
n.clear()
print("What number are you converting to a percent? (AS A DECIMAL)")
inp = input()
n.append(inp)
per = float(n[0]) * 100
sent = "{0}%".format(per)
print(sent)
return main()
#Circumfrence function
def cir():
n.clear()
inp = input("Do you have radius (r) or diameter (d)?\n")
if inp.lower() == 'r':
print("What is the radius?")
r = input()
val = 2 * m.pi * float(r)
print(val)
return main()
elif inp.lower() == 'd':
inp = input()
print("What is the diameter?")
r = input()
val = 2 * m.pi * float(r)
print(val)
return main()
#Area function
def ar():
n.clear()
print("What is the length?")
inp = input()
n.append(inp)
print("What is the height?")
inp = input()
n.append(inp)
prod = float(n[0]) * float(n[1])
print(prod)
return main()
# Circle Area function
def car():
print("Do you have the diameter (d) or the radius (r)?")
inp = input()
if inp.lower() == 'r':
print("What is the radius of the circle?")
r = input()
val = m.pi * float(r) ** 2
print(val)
return main()
elif inp.lower() == 'd':
print("What is the diameter of the circle?")
d = input()
r = float(d) / 2
val = m.pi * float(r) ** 2
print(val)
return main()
#Useless as of right now
def timerr():
return main()
#Runs the calculator on startup
if __name__ == "__main__":
main()
Fixed "Too high!" and "Too low!" printing (ex. guess == 5, secret_number == 10, output: "Too high!"). I also removed printing the guess and the secret_number, as that a) wasted some output space, and b) gave the answer away. I would do more, but I can't code for crap lol
from random import randint def main(): GUESS_LIMIT = 10 while True: secret_number = randint(1, 100) for guess_count in range(GUESS_LIMIT): while True: # Validating user input guess = input('Guess a number between 1 and 100 \n> ') if guess.isdigit() and 1 <= int(guess) <= 100: break else: print('Invalid input!') if int(guess) == secret_number: print(f'Congratulations! You guessed the secret number in {guess_count+1} guesses') break elif int(guess) < secret_number: print(f"Too low!") else: print("Too high!") else: print(f'Game over! The secret number was {secret_number}.') if play_again() is False: break def play_again(): # Ask the player if they want to play again while True: user_input = input('Do you want to play again (yes or no)?\n> ') if user_input.isalpha(): if user_input.lower() == 'yes': return True elif user_input.lower() == "no": return False print('Invalid input!') if __name__ == '__main__': main()
- from random import randint
- def main():
- GUESS_LIMIT = 10
- while True:
- secret_number = randint(1, 100)
- for guess_count in range(GUESS_LIMIT):
- while True:
- # Validating user input
- guess = input('Guess a number between 1 and 100 \n> ')
- if guess.isdigit() and 1 <= int(guess) <= 100:
- break
- else:
- print('Invalid input!')
- if int(guess) == secret_number:
- print(f'Congratulations! You guessed the secret number in {guess_count+1} guesses')
- break
elif int(guess) > secret_number:print(int(guess), secret_number)- elif int(guess) < secret_number:
- print(f"Too low!")
- else:
- print("Too high!")
- else:
- print(f'Game over! The secret number was {secret_number}.')
- if play_again() is False:
- break
- def play_again():
# Ask the player if he wants to play again- # Ask the player if they want to play again
- while True:
- user_input = input('Do you want to play again (yes or no)?\n> ')
- if user_input.isalpha():
- if user_input.lower() == 'yes':
- return True
- elif user_input.lower() == "no":
- return False
- print('Invalid input!')
- if __name__ == '__main__':
import codewars_test as test # TODO Write tests import solution # or from solution import example # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("test case") def test_case(): test.assert_equals(1 + 1, 2)
- import codewars_test as test
- # TODO Write tests
- import solution # or from solution import example
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
- test.assert_equals(1 + 1, 2)