How would I make this more compact and faster? New learner - python

Sorry to ask a very long question but I am very new to Python. I started a free course on FutureLearn and a task was to create a calculator. The code I had ended up being a lot longer than other answers and it was too big to fit into the comment section. Just looking for tips for any part of my code that could be squished down. First question on StackOverflow so I'm sorry if this is a bad question.
def main():
operations = ['multiplication', 'division', 'addition', 'subtraction', 'x^y']
def find_op():
while True:
try:
operation = input('What do you want to do? Addition, subtraction, multiplication, division, or x^y??\n')
operations.index(operation.lower())
break
except ValueError:
input('Error!, you must input one of the five options, enter to try again.')
return operations.index(operation.lower())
operation_index = find_op()
match operation_index:
case 0:
num_question_ending = 'multiply with'
case 1:
num_question_ending = 'divide with'
case 2:
num_question_ending = 'add with'
case 3:
num_question_ending = 'subtract with'
case 4:
num_question_ending = 'create an exponential with'
def add():
add_ans = global_number1 + global_number2
print(f'{global_number1} + {global_number2} =\n{add_ans}')
def sub():
sub_ans = global_number1 - global_number2
print(f'{global_number1} - {global_number2} =\n{sub_ans}')
def mult():
mult_ans = global_number1 * global_number2
print(f'{global_number1} multiplied by {global_number2} =\n{mult_ans}')
def div():
while True:
try:
div_ans = global_number1 / global_number2
break
except ZeroDivisionError:
input('Naughty naughty boy trying to divide by 0. '
'Now you gonna have to restart the code. Press enter plz')
main()
print(f'{global_number1} divided by {global_number2} =\n{div_ans}')
def power():
if global_number1 == 0 and global_number2 == 0:
input('Naughty boy trying 0^0, dat is undefined boi. Enter to restart the whole thing.')
main()
pow_ans = global_number1 ** global_number2
print(f'{global_number1} to the power of {global_number2} =\n{pow_ans}')
def get_number1():
while True:
try:
numba1 = input(f'what\'s the first number you want to {num_question_ending}??\n')
float(numba1)
break
except ValueError:
input('input must be a number. enter to try again.')
numba1 = float(numba1)
return numba1
def get_number2():
while True:
try:
numba2 = input(f'what\'s the second number you want to {num_question_ending}??\n')
float(numba2)
break
except ValueError:
input('input must be a number. enter to try again.')
numba2 = float(numba2)
return numba2
global_number1 = get_number1()
global_number2 = get_number2()
match operation_index:
case 0:
mult()
case 1:
div()
case 2:
add()
case 3:
sub()
case 4:
power()
def repeat():
go_again_ans = input('would you like to go again? Y/N\n')
if go_again_ans == 'Y':
main()
elif go_again_ans == 'N':
exit()
else:
input('Error! You need to answer with either Y or N, enter to try again.')
repeat()
repeat()
if __name__ == '__main__':
main()

I think this is the most concise way
I used the menu prompt to save the text, first class functions to not search for the operation and a lambda functions to avoid defining each function in 3 lines, also not checking for the zero I only catch the exception
I am also still learning so excuse me if something is off
Thanks
menu_prompt = f"""
Please enter:
'a' For addition
's' For Subtraction
'm' For Multiplication
'd' For Division
'p' For Exponential operations
'q' To quit
"""
add = lambda x, y: f"{x} + {y} = { x + y}"
subtract = lambda x, y: f"{x} - {y} = { x - y}"
multiply = lambda x, y: f"{x} * {y} = { x * y}"
divide = lambda x, y: f"{x} / {y} = { x / y:.2f}"
power = lambda x, y: f"{x} ^ {y} = { x ** y}"
operations = {
'a': add,
's': subtract,
'm': multiply,
'd': divide,
'p': power
}
def get_number(order):
while True:
try:
return float(input(f"Please enter the {order} number: "))
except ValueError:
print("Please enter a valid number")
def run_selected_operation(operation):
x = get_number("first")
y = get_number("second")
try:
print(operation(x, y))
except ZeroDivisionError:
print("Can not divide by zero, try again")
user_selection = input(menu_prompt)
while user_selection.lower() != 'q':
if user_selection in operations:
selected_operation = operations[user_selection]
run_selected_operation(selected_operation)
else:
print("Please enter a valid selection")
user_selection = input(menu_prompt)
user_selection = input(menu_prompt)
print(menu_prompt)

Related

Returning "0" if given an empty list

My task is to make a Program that finds the median, mode and mean of a user defined number set. I believe that part is done, however it is also suppose to return a "0" if the list is empty. The way i am deriving my number list is through
enterNumbers = list(map(int,input("Enter Numbers : ").strip().split(' ')))[:]
This returns ValueError: invalid literal for int() with base 10: ''
Obviously this will only accept integers as an argument but I don't know where to begin to remedy this.
Full code as follows:
#Define median , Mode and Mean
def median(enterNumbers):
enterNumbers.sort()
middle = len(enterNumbers) // 2
if len(enterNumbers) % 2 == 1:
return enterNumbers[middle]
else:
return enterNumbers[middle] + enterNumbers[middle - 1] / 2
def mode(enterNumbers):
numberDictionary = {}
for digit in enterNumbers:
number = numberDictionary.get(digit, None)
if number == None:
numberDictionary[digit] = 1
else:
numberDictionary[digit] = number + 1
maxValue = max(numberDictionary.values())
modeList = []
for key in numberDictionary:
if numberDictionary[key] == maxValue:
modeList.append(key)
return modeList
def mean(enterNumbers):
mean = 0
for num in enterNumbers:
mean += num
return mean / len(enterNumbers)
#Define Main Function
def main():
enterNumbers = list(map(int,input("Enter Numbers : ").strip().split(' ')))[:]
enterNumbers.sort()
print("\n"," Number List ","\n---------------------","\n",enterNumbers)
print("\n","median, mode mean?","\n---------------------")
answer = input()
if answer == "median":
answer = median
elif answer == "mode":
answer = mode
elif answer == "mean":
answer = mean
print(answer(enterNumbers))
main()
I would highly recommend to refactor your code. But that would fix your error.
enterNumbers = list(map(int, (input("Enter Numbers : ") or "0").strip().split(' ')))[:]
You can just use a 'Try Except' in this part of your code. Will treat errors in general, even if the user types a String.
def main():
try:
enterNumbers = list(map(int, input("Enter Numbers : ").strip().split(' ')))[:]
enterNumbers.sort()
print("\n", " Number List ", "\n---------------------", "\n", enterNumbers)
print("\n", "median, mode mean?", "\n---------------------")
answer = input()
if answer == "median":
answer = median
elif answer == "mode":
answer = mode
elif answer == "mean":
answer = mean
print(answer(enterNumbers))
except:
print(0)
This will work with you :
def main():
try:
enterNumbers = list(map(int, input("Enter Numbers : ").strip().split(' ')))[:]
enterNumbers.sort()
print("\n", " Number List ", "\n---------------------", "\n", enterNumbers)
print("\n", "median, mode mean?", "\n---------------------")
answer = input()
except:
print('Please Enter a number')
main()
if answer == "median":
answer = median
elif answer == "mode":
answer = mode
elif answer == "mean":
answer = mean
print(answer(enterNumbers))
I'd handle this by having your list default to [0] if there's no valid input. That will produce the desired result of 0 from all of the averaging functions.
from statistics import mean, median, mode
def main():
try:
numbers = [int(n) for n in input("Enter Numbers: ").split()]
except ValueError:
numbers = [0]
func = {
"mean": mean,
"median": median,
"mode": mode,
}
print(f"Number list: {numbers}")
print(func[input("median, mode, mean? ")](numbers))
if __name__ == '__main__':
main()
I've used the statistics module for brevity, but it should work exactly the same with your hand-rolled implementations.

How to better implement a history of user action?

I decided to make a calculator as a project.
Implementing basic addition, subtraction, division, and multiplication was fairly easy.
I wanted to add more functionality so I decided to implement a list of results the user view. However, I had a difficult time keeping track of the results numerically. I wrote a maze of if statements that are functional but seem to be overwrought with code. I am sure there is a better way to handle this.
Any advice?
def add(x, y):
return x + y
def sub(x, y):
return x - y
def mul(x, y):
return x * y
def div(x, y):
value = None
while True:
try:
value = x / y
break
except ZeroDivisionError:
print('Value is not dividable by 0, try again')
break
return value
def num_input(prompt='Enter a number: '):
while True:
try:
print(prompt, end='')
x = int(input())
break
except ValueError:
print('You must input a number. Try again.')
return x
def get_two_val():
x, y = num_input(), num_input()
return x, y
print("Welcome to Simple Calc")
# declaration of variables
num_of_calc_counter = 0
index_of_calc = 1
calculations = []
while True:
print("Choose from the following options:")
print(" 1. Add")
print(" 2. Subtract")
print(" 3. Multiply")
print(" 4. Divide")
print(" 5. Sales Tax Calculator")
print(" 6. Recent Calculations")
print(" 0. Quit")
usrChoice = num_input('Enter your choice: ')
'''
Menu workflow
options 1-4 take in two numbers and perform the specified calculation and
then add the result to a master list that the user can reference later.
lastly, the workflow increments the num_of_calc variable by 1 for recent
calc logic
option 5 is a simple tax calculator that needs work or option to enter
or find tax rate
option 6 returns a list of all the calculations perform by the user
'''
if usrChoice is 1:
numbers = get_two_val()
result = add(*numbers)
print(numbers[0], "plus", numbers[1], "equals", result)
calculations.extend([result])
num_of_calc_counter += 1
elif usrChoice is 2:
numbers = get_two_val()
result = sub(*numbers)
print(numbers[0], "minus", numbers[1], "equals", result)
calculations.extend([result])
num_of_calc_counter += 1
elif usrChoice is 3:
numbers = get_two_val()
result = mul(*numbers)
print(numbers[0], "times", numbers[1], "equals", result)
calculations.extend([result])
num_of_calc_counter += 1
elif usrChoice is 4:
numbers = get_two_val()
result = div(*numbers)
print(numbers[0], "divided by", numbers[1], "equals", result)
calculations.extend([result])
num_of_calc_counter += 1
elif usrChoice is 5:
tax_rate = .0875
price = float(input("What is the price?: "))
total_tax = tax_rate * price
final_amount = total_tax + price
print('Tax rate: ', tax_rate, '%')
print('Sales tax: $', total_tax)
print('_____________________________')
print('Final amount: $', final_amount)
#
elif usrChoice is 6:
if len(calculations) is 0:
print('There are no calculations')
elif num_of_calc_counter == 0:
index_of_calc = 1
for i in calculations:
print(index_of_calc, i)
index_of_calc += 1
num_of_calc_counter += 1
elif index_of_calc == num_of_calc_counter:
index_of_calc = 1
for i in calculations:
print(index_of_calc, i)
index_of_calc += 1
num_of_calc_counter += 1
elif num_of_calc_counter > index_of_calc:
index_of_calc = 1
for i in calculations:
print(index_of_calc, i)
index_of_calc += 1
num_of_calc_counter -= 1
elif num_of_calc_counter < index_of_calc:
index_of_calc = 1
for i in calculations:
print(index_of_calc, i)
index_of_calc += 1
num_of_calc_counter += 1
elif usrChoice is 0:
break
I don't know if you could find this simpler:
def num_input(prompt='Enter a number: '):
finished = False
while not finished:
string_input = input(prompt)
try:
input_translated = int(string_input)
except ValueError:
print('You must input a number. Try again.')
else:
finished = True
return input_translated
def division_operation(x, y):
if y == 0:
print('Value is not dividable by 0, try again')
return None
else:
return x / y
math_operations_values = [
(lambda x, y: x + y, 'plus'),
(lambda x, y: x - y, 'minus'),
(lambda x, y: x * y, 'times'),
(division_operation, 'divided by')
]
def get_two_val():
return (num_input(), num_input())
def operate_on_numbers(operation_index):
def operate():
numbers = get_two_val()
operator, operation_string = math_operations_values[operation_index]
result = operator(*numbers)
if result is not None:
print(numbers[0], operation_string, numbers[1], "equals", result)
calculations.append(result)
return operate
def tax_computation():
tax_rate = .0875
price = float(input("What is the price?: "))
total_tax = tax_rate * price
final_amount = total_tax + price
print('Tax rate: ', tax_rate * 100, '%')
print('Sales tax: $', total_tax)
print('_____________________________')
print('Final amount: $', final_amount)
def show_computations():
if calculations:
for (index, values) in enumerate(calculations, start=1):
print(f'{index}: {values}')
else:
print('There are no calculations')
calculations = []
finished = False
choices_actions = [
operate_on_numbers(0),
operate_on_numbers(1),
operate_on_numbers(2),
operate_on_numbers(3),
tax_computation,
show_computations
]
while not finished:
print("""
Choose from the following options:
1. Add
2. Subtract
3. Multiply
4. Divide
5. Sales Tax Calculator
6. Recent Calculations
0. Quit""")
user_choice = num_input('Enter your choice: ')
'''
Menu workflow
options 1-4 take in two numbers and perform the specified calculation and
then add the result to a master list that the user can reference later.
lastly, the workflow increments the num_of_calc variable by 1 for recent
calc logic
option 5 is a simple tax calculator that needs work or option to enter
or find tax rate
option 6 returns a list of all the calculations perform by the user
'''
if user_choice == 0:
finished = True
else:
try:
operation_to_do = choices_actions[user_choice - 1]
except IndexError:
print('Please enter one of choice shown.')
else:
operation_to_do()

Begginer- Doesn't let me input 'x'

Doesn't let me to input values to x.
It runs and it says that is completed with exit code 0. Why?
def donuts():
x = int(input("How many donutes?"))
if x < 10:
print("Numbers of donuts: "+ x)
else:
print("Number of donute: many")
def litere():
x = input("Type a word! : ")
if len(x) <= 2:
print("NULL")
else:
k = len(x) -1
print(x[0:3] + x[k-2])
if ' name ' == ' main ':
donuts()
__name__ is a variable. It should have quotes around it. When you surround it with quotes python treats it like a regular string. Replace the bottom with
if __name__ == "__main__":
donuts()
Remove quotes around '__name__' so it becomes __name__:
def donuts():
x = int(input("How many donutes?"))
if x < 10:
print("Numbers of donuts: "+ x)
else:
print("Number of donute: many")
def litere():
x = input("Type a word! : ")
if len(x) <= 2:
print("NULL")
else:
k = len(x) -1
print(x[0:3] + x[k-2])
if __name__ == "__main__":
donuts()
Here is the change I made to your code:
if __name__ == "__main__":
donuts()

Can you use break to validate input in python?

Like using this to validate that an input is only alpha-numeric:
while True:
str = input('')
if str.isalnum():
break
else:
print("Please include only alpha-numeric characters.\n")
This code has worked for all instances that I have tested it in, but is this bad practice?
That's fine. Here is a note, however: you can find out if the while loop exited with a break or without one by using else:
x = 0
while x < 4:
x += 1
else:
print("no break")
# prints no break
If you break, however:
x = 0
while x < 4:
x += 1
if x == 2:
break
else:
print("no break")
# Does not print
you can abstract it further
def verified_input(prompt='',test_condition=lambda x:1,err_message="Please Enter Valid Input"):
while True:
result = input(prompt)
if test_condition(result):
return result
print( err_message )
def verified_alnum(prompt,err_message="Please enter Only alpha numerics"):
return verified_input(prompt,lambda x:x.isalnum(),err_message)
result = verified_alnum("Enter Password:","A password must be only letters and numbers")
this allows you to create any number of test conditions quickly and relatively verbosely

Division by zero error when adding rogue value without any data

Hi having trouble trying to fix an error that occurs when I put just a '#' or rogue value in case someone doesn't want to add any data. I don't know how to fix it and I'm hoping to just end the code just like I would with data.
#Gets Data Input
def getData():
fullList = []
inputText = checkInput("Enter the students first name, last name, first mark, and second mark (# to exit): ")
while inputText != "#":
nameList = []
nameList2 = []
nameList = inputText.split()
nameList2.extend((nameList[0],nameList[1]))
nameList2.append((float(nameList[2]) + float(nameList [3]))/2)
fullList.append(nameList2)
inputText = checkInput("Enter the students first name, last name, first mark, and second mark (# to exit): ")
print("\n")
return fullList
#Calculates Group Average
def calc1(fullList):
total = 0
for x in fullList:
total = total + x[2]
groupAverage = total/(len(fullList))
return(groupAverage)
#Finds Highest Average
def calc2(fullList):
HighestAverage = 0
nameHighAverage = ""
for x in fullList:
if x[2] > HighestAverage:
HighestAverage = x[2]
nameHighAverage = x[0] + " " + x[1]
return (HighestAverage, nameHighAverage)
#Returns Marks above average
def results1(groupAverage,r1FullList):
r1FullList.sort()
print("List of students with their final mark above the group average")
print("--------------------------------------------------------------")
print("{:<20} {:<12}".format("Name","Mark"))
for x in r1FullList:
if x[2] > groupAverage:
name = x[0] + " " + x[1]
print("{:<20} {:<12.2f}".format(name,x[2]))
def calc3(x):
if x[2] >= 80:
return 'A'
elif x[2] >= 65:
return 'B'
elif x[2] >= 50:
return 'C'
elif x[2] < 50:
return 'D'
else:
return 'ERROR'
def results2(fullList):
print("List of Studens with their Final Marks and Grades")
print("-------------------------------------------------")
print("{:<20} {:<12} {:<12}".format("Name","Mark","Grade"))
for x in fullList:
grade = calc3(x)
name = x[0] + " " + x[1]
print("{:<20} {:<12.2f} {:<12}".format(name,x[2],grade))
#Checks for boundary and invalid data
def checkInput(question):
while True:
textInput = input(question)
if textInput == "#":
return textInput
splitList = textInput.split()
if len(splitList) !=4:
print("Invalid Format, Please Try Again")
continue
try:
a = float(splitList[2])
a = float(splitList[3])
if float(splitList[2]) < 0 or float(splitList[2]) > 100:
print("Invalid Format, Please Try Again")
continue
if float(splitList[3]) < 0 or float(splitList[3]) > 100:
print("Invalid Format, Please Try Again")
continue
return(textInput)
except ValueError:
print("Invalid Input, Please Try Again")
continue
#Main Program
#Input Data
fullList = getData()
#Process Data
groupAverage = calc1(fullList)
HighestAverage, nameHighAverage = calc2(fullList)
#Display Results
print("The group average was %.2f" % groupAverage)
print("The student with the highest mark was: %s %0.2f" %(nameHighAverage,HighestAverage))
results1(groupAverage,fullList)
print("\n")
results2(fullList)
Your program works OK for me, unless you enter a # as the first entry, in which case fullList is [] and has length 0. Hence, DivisionByZero at this line: groupAverage = total/(len(fullList)).
You could modify your code to check for this and exit:
import sys
fullList = getData()
if not fullList:
print('No Data!')
sys.exit()

Categories