Python Forum
user defined list-based calculator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
user defined list-based calculator
#1
Just a simple, strange calc...basic functions.I haven't taken it for a spin as yet, but it is simple... Pt of it is to use reduce() and lambda since its pretty much the only function that can't easily be replicated with list comprehension.

Also on my github (mepyyeti)



#!usr/bin/env python3
#simplcalc0.py

import os
from functools import partial

def askme(question,type,errmsg):
	while True:
		user_input = input(question)
		if user_input != '':
			try:
				user_input == type(user_input)
			except ValueError:
				print(f'{user_input} is insufficient. Please read:')
				print(errmsg)
			else:
				print(f'{user_input} is acceptable.')
				return user_input
		else:
			continue

def calculator():
	while True:
		print(f'you are here: {os.getcwd()}')
		
		dirch = partial(askme,type=str,errmsg='\'s\'to stay in this directory\ntype new dir address to change')
		dirname=dirch('enter dir? ')
		if dirname != 's':
			os.chdir(dirname)
			print(os.getcwd())
		
		while True:
			print('''your options:\n
			1.  add
			2.  subtract
			3.  multiply
			4.  divide
			''')
			
			num_list = []
			
			menu_selection = partial(askme,type=int,errmsg='please enter NUMBER choice. Thank you.')
			menu = menu_selection('please choose from above menu. >>  ')
			if menu > 4 or menu <= 0:
				print('please read menu carefully...')
				continue
		
		list_make = partial(askme, type=int, errmsg='must be whole numbers.')
		while True:
			populate=list_make('enter a whole number...')
			num_list.append(populate)
			
			keep_going=partial(askme, type=str, errmsg='must be either \'y\' or \'n\'.')
			keep_populating =  keep_going('want to keep inserting data (aka numbers)?')
			if keep_populating != 'y':
				break
		
		if menu == 1:
			operation = reduce(lambda a,b: a+b,num_list)
		elif menu == 2:
			operation = reduce(lambda a,b: a-b, num_list)
		elif menu == 3:
			operation = reduce(lambda a,b: a*b, num_list)
		elif menu == 4:
			operation = reduce(lambda a,b: a/b, num_list)
		
		
		
		file_name = partial(askme, type=str, errmsg='please remember extension...')
		filen = file_name('please enter a filename with extension >> .txt is a good choice.') 
		with open(filen, 'a+') as f:
			f.write('your values are:')
			for k,v in enumerate(num_list):
				f.write(k,'.  ',v)
			f.write(f'your result is: {operation}')
		
		print('thank you....')
		
		print('care to continue with a new data set?')
		choice = partial(askme,type=str,errmsg='please choose [y/n]')
		choice_input = choice('continue or leave? [y/n]')
		if choice_input == 'y' or choice_input == 'yes':
			continue
		else:
			break

if __name__=='__main__':
	calculator()
else:
	print('sorry no can do...')
EDIT: fixed line 54 (geany quirk gotta type vars) thanks almenon.
Reply
#2
You are missing a _ in line 54 "keep going"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  user friendly making a list and changing it hobbyprogrammer 2 2,948 Nov-10-2018, 03:14 PM
Last Post: hobbyprogrammer
  Avoiding new user list sorting errors ljmetzger 1 2,729 Apr-07-2018, 04:03 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020