Below is the current working code for an ongoing project.
I am wondering if there is anything I can do to improve the code snippet below. My main concerns lie to two area. First being that I append the answer of an input to a list called inputList and then call it in a block of conditionals. I am wondering if this in particular is a good approach. Are there any better approaches?
Secondly, similar concerns lay with how I defined the answers and dictionaries of their variants. My main purpose there is that, regardless of how you enter "yes" or "no", as long as it has those letters and in the proper order, i.e "nO" or "yES", it would be accepted. I also did it as I did so that I would not have to concern myself on checking whether or not the first letter of any of those two words are capitalised or not.
Again, my question being, is this a "solid" approach? What alternative approaches would be better for this snippet of code? Perhaps a better question would be, how can I improve this snippet?
I am also interested in seeing if I can shorten it as much as possible. Similar to how permLet() is written.
from itertools import product, zip_longest
import pylab as plot
import numpy as number
import pprint
inputFile = input('File Name: ')
def yLimits():
yMin = int(input('Set lower limit: '))
yMax = int(input('Set upper limit: '))
labelLocation = number.arange(len(count))
plot.bar(labelLocation, list(count.values()), align='center', width=0.5)
plot.xticks(labelLocation, list(count.keys()))
plot.xlabel('Characters')
plot.ylabel('Frequency')
plot.ylim(yMin, yMax)
plot.show()
def ylimChoice():
#returns all permutations of letter capitalisation in a certain word.
def permLet(s):
return(''.join(t) for t in product(*zip(s.lower(), s.upper())))
inputList = []
yesNo = input('Would you like to set custom ylim() arguments? ')
inputList.append(yesNo)
yes = list(permLet("yes"))
no = list(permLet("no"))
if any(yesNo in str({y: y for y in yes}.values()) for yesNo in inputList[0]):
yLimits()
elif any(yesNo in str({n: n for n in no}.values()) for yesNo in inputList[0]):
labelLocation = number.arange(len(count))
plot.bar(labelLocation, list(count.values()), align='center', width=0.5)
plot.xticks(labelLocation, list(count.keys()))
plot.xlabel('Characters')
plot.ylabel('Frequency')
plot.autoscale(enable=True, axis='both', tight=False)
plot.show()
count = { }
with open(inputFile, 'r') as info:
readFile = info.read()
for character in readFile.upper():
count.setdefault(character, 0)
count[character] = count[character]+1
value = pprint.pformat(count)
print(value)
ylimChoice()