Python dictionary if/else invalid syntax - python

I cant figure out why line 104 keeps returning invalid syntax, can someone please point me in the right direction? Does it have something to do with the way i used elif? Sorry if this is a newbie question!
Line 104 is the else statement in the for item in workingDict.keys() loops inside printGrammar
import sys
import string
from collections import defaultdict
#default length of 3
stringLength = 3
#get last argument of command line(file)
if len(sys.argv) == 1:
#get a length from user
try:
stringLength = int(input('Length? '))
filename = input('Filename: ')
except ValueError:
print("Not a number")
elif len(sys.argv) == 2:
#get a length from user
try:
stringLength = int(input('Length? '))
filename = sys.argv[1]
except ValueError:
print("Not a number")
elif len(sys.argv) == 3:
filename = sys.argv[2]
stringLength = sys.argv[1].split('l')[1]
else:
print("Invalid input!")
#get start symbol
with open(filename, "r") as grammar:
#read file
lines = grammar.readlines()
start = lines[0].split('=')[0]
start = start.replace(" ", "")
#checks
#print(stringLength)
#print(filename)
#print(start)
def str2dict(filename):
result = defaultdict(list)
with open(filename, "r") as grammar:
#read file
lines = grammar.readlines()
count = 0
#loop through
for line in lines:
#append info
line = line.rstrip()
result[line[0]].append(line.split('=')[1])
return result
workingDict = str2dict("Binary.txt")
print(workingDict)
def printGrammar(result):
sentenceList = []
workList = []
workList.append(start)
i = 0
firstNT = ""
#While the worklist is not empty:
while(len(workList) != 0):
#Get and delete one potential sentence s from the worklist.
symbol = workList.pop()
#If the | s | > N, continue.
if len(str(symbol).replace(" ", "")) > int(stringLength):
continue
else:
if str(symbol) in workingDict.keys():
#append the right derivation
for item in workingDict.get(symbol):
workList.append(list(item.replace(" ", "")))
#workList.append(str(workingDict.get(symbol)))
#add derivation
print(workingDict.keys())
#If s has no nonterminals, print s and continue.
for item in workingDict.keys():
print("test")
print(''.join(item))
if len(item) != 1:
continue
#if the element is in dictionary, dont print
elif ''.join(item) in workingDict.keys():
continue
print(symbol)
#if element is not in dictionary, print
else:
print("THIS IS A TERMINAL!!")
print(item)
#Choose the leftmost nonterminal NT.
print(workList)
#For all productions NT -> rhs:
#Replace NT in s with rhs; call it tmp.
#Store tmp on worklist.
return workList
print (printGrammar(workingDict))

You need to indent the line
print(symbol)
to the same level as continue.

Related

How to run the same python code for multiple input files

I have two output files which contain the same data (but different values). I'm using the following Python code to read them and return the data/value that I want:
upper = input("Enter file name (upper): ")
lower = input("Enter file name (lower): ")
fhr = open(upper)
for line in fhr:
word = line.rstrip().split()
if len(word) > 1 and word[1] == '1:47':
try:
sabs = word[2]
except:
continue
tot_upper = float(sabs)
print('Total upper:', tot_upper)
fhr.close()
fhr = open(lower)
for line in fhr:
word = line.rstrip().split()
if len(word) > 1 and word[1] == '1:47':
try:
sabs = word[2]
except:
continue
tot_lower = float(sabs)
print('Total lower:', tot_lower)
fhr.close()
Which gives me the output:
Total upper: x
Total lower: y
Is there are way that I can simplify the code such that I open the first file, run the code, then loop back to the beginning, open the second file and run the same code? Something like this:
upper = input("Enter file name (upper): ")
lower = input("Enter file name (lower): ")
file = [upper, lower]
for inp in file:
fhr = open(file)
for line in fhr:
word = line.rstrip().split()
if len(word) > 1 and word[1] == '1:47':
try:
sabs = word[2]
except:
continue
if inp == upper:
tot_upper = float(sabs)
print('Total upper:', tot_upper)
elif inp == lower:
tot_lower = float(sabs)
print('Total lower:', tot_lower
fhr.close()
I still want the same output:
Total upper: x
Total lower: y
You could do this:
for label in 'upper', 'lower':
# Ask for filename, using the label.
# Process the file.
# Print the result, using the label.
You probably need to use functions:
upper = input("Enter file name (upper): ")
lower = input("Enter file name (lower): ")
def f(name, s): # idk what is the function for so change to a custom name
fhr = open(name)
for line in fhr:
word = line.rstrip().split()
if len(word) > 1 and word[1] == '1:47':
try:
sabs = word[2]
except:
continue
tot = float(sabs)
print(f'Total {s}:', tot)
fhr.close()
f(upper, 'upper')
f(lower, 'lower')

How to extract data from a txt.readlines()?

I have a code that seems to have a slight issue it reads the file... But the issue is it can not identify the words/letters this is what i mean extract('test.txt',('A','E','I','O','U')) the test.txt is a txt file and the ('A','E','I','O','U') is the words i want to find.
my code:
def extract(file,find=()):
with open(str(file),'r') as file:
find = list(find)
read_file = file.readlines()
num1 = 0
num = num1-1
for vals in find:
num1 += 1
pass
for finds in read_file:
linef = str(finds)
main_obj = find[num]
if main_obj in linef:
print("Yay")
elif linef != main_obj:
print("NO")
else:
print("???")
pass
extract('test.txt',('A','E','I','O','U'))
What my output is:
NO
NO
NO
NO
Yay
This is the expected output:
Yay
Yay
Yay
Yay
Yay
inside test.txt:
A
E
I
O
U
I'll just leave this here. If the end result is to compare each variable inside of find to each line in file then this should do the job:
def extract(file, find=()):
with open(file) as F:
contents = F.readlines()
for line in contents:
if any([i in line for i in find]):
print("Yay")
else:
print("NO")
extract("test.txt", ('A','E','I','O','U'))
You just need to update your logic and remove this code. because it will always return -1 index on every increment.
num1 = 0
num = num1-1
for vals in find:
num1 += 1
pass
Just use one variable and increment it accordingly.
def extract(file, find=()):
num = 0
with open(str(file), 'r') as file:
find = list(find)
print(find)
read_file = file.read().splitlines()
print(read_file)
for finds in read_file:
linef = str(finds)
main_obj = find[num]
num += 1
if main_obj in linef:
print("Yay")
elif linef != main_obj:
print("NO")
else:
print("???")
pass
In the code:
for finds in read_file:
linef = str(finds)
main_obj = find[num]
if main_obj in linef:
print("Yay")
elif linef != main_obj:
print("NO")
you never increment num and hence are essentially always comparing find[-1] with main_obj. I guess you have to increment num if there is a match or whatever your requirement is.
if main_obj in linef:
num += 1
print("Yay")
Either way, this would be the most down-to-earth correct way to do what you are required to:
for temp in find:
not_found = True
for finds in read_files:
if temp in finds:
not_found = False
print("Yay")
break
elif temp != finds:
not_found = True # elif is not needed TBH but just adding for clarity
if not_found:
print("NO")

How can I make it where it will print the longest name/string that was inputed by the user

def myNames():
names = []
while True:
a = input("Enter Name: ")
if a != "done":
names.append(a)
elif a == "done":
return names
def all_lengths(myNames):
num_of_strings = len(myNames)
total_size = 0
for item in myNames:
total_size += len(item)
ave_size = float(total_size) / float(num_of_strings)
print(ave_size)
all_lengths(myNames())
def longestWord(myNames):
count = 0
for i in myNames:
if len(i) > count:
count = len(i)
word = I
print ("the longest string is ", word)
how can I make it print the longest name that was inputted by the user for example: out of Samantha and John it would say that Samantha was the longest name
longestName = ""
for name in myNames:
if len(name) > len(longestName):
longestName = name
print("The longest name is", longestName)
This will check the length of each username, and if the name of that username is longer than the current longest username, it will replace it.
You already have the function for it.
Just need to call the function longestWord().
Write longestWord(myNames()) at the end of the program. Right after,
def longestWord(myNames):
count = 0
for i in myNames:
if len(i) > count:
count = len(i)
word = i # Need to type I in lower case
print ("the longest string is ", word)
Update: Since, you don't want the function to ask for names again, you can move the function calling longestWord()inside the above function where it calculates average, with the parameter as myNames i.e.
def myNames():
names = []
while True:
a = input("Enter Name: ")
if a != "done":
names.append(a)
elif a == "done":
return names
def longestWord(myNames):
count = 0
for i in myNames:
if len(i) > count:
count = len(i)
word = i
print ("the longest string is ", word)
def all_lengths(myNames):
num_of_strings = len(myNames)
total_size = 0
for item in myNames:
total_size += len(item)
ave_size = float(total_size) / float(num_of_strings)
print(ave_size)
longestWord(myNames) # Calling the function with already given names
all_lengths(myNames())

Write list to 2 different text files

Reading in the data text file,
Running loops to check criteria for valid and invalid student numbers.
Then trying to write 2 text files, one for the invalid student numbers and one for the valid student numbers
This far everything works accept no text files written and no data written to text files at the end
Here is the Script so far
inputList = []
outputList = []
def FileOpen(myList):
#try:
count=0
INFILE=open("data.txt", "r")
for line in INFILE:
myList.append(line.rstrip())
count+=1
INFILE.close()
return count
#except:
# print("File could not be found")
# exit()
FileOpen(inputList)
print(FileOpen(inputList),"number of lines read from file")
def AnalyseStudents(rawStudents,totalStudents):
for entry in rawStudents:
amountOfDigits = len(entry)
testOfDigits = entry.isdigit()
def inValid1(val1,val2):
answ = val1 != 8 and val2 != True
return answ
inValidResult1=(inValid1(amountOfDigits,testOfDigits))
def Valid1(val1,val2):
answ = val1 == 8 and val2 == True
return answ
validResult1=(Valid1(amountOfDigits,testOfDigits))
if inValidResult1:
print(entry, " is an INVALID student number")
elif validResult1:
a=entry[0]
b=entry[1]
c=entry[2]
d=entry[3]
e=entry[4]
f=entry[5]
g=entry[6]
h=entry[7]
sum = float((a*8)+(b*7)+(c*6)+(d*5)+(e*4)+(f*3)+(g*2)+(h*1))
result = sum%11
def inValid2 (val):
answ = val != 0
return answ
inValidResult2=(inValid2(result))
def Valid2 (val):
answ = val == 0
return answ
validResult2=(Valid2(result))
if validResult2:
print(entry, " is an VALID student number")
elif inValidResult2:
print(entry, " is an INVALID student number")
totalStudents
AnalyseStudents(inputList,outputList)
def Write(outList):
if outList == (" is an VALID student number"):
OUTFILE1=open("ValidNumbers.txt","w")
for validResult in outList:
OUTFILE1.write(validResult+"\n")
OUTFILE1.close()
elif outList == (" is an INVALID student number"):
OUTFILE2=open("InvalidNumbers.txt","w")
for inValidResults in outList:
OUTFILE2.write(inValidResults+"\n")
OUTFILE2.close()
Write(outputList)
print("See output files for more details")
Your equality statements are wrong
if outList == (" is an VALID student number"):
...
elif outList == (" is an INVALID student number"):
The outList is a list but you are comparing it to strings. Put a debug breakpoint at those if statements and decide what you want.
I'm still working my way through this, but for the first function, you can use list comprehension and a couple of other Python functions. Commentary is included.
inputList = []
outputList = []
# Change parameter to the name of the file you want to open.
# Here, we can just make the parameter optional and pass your
# text file name by default.
def FileOpen(filename="data.txt"):
# Using with keeps the open file in context until it's finished
# it will then close automatically.
with open(filename, "r") as INFILE:
# Reach all the data at once and then try to decode it.
data = INFILE.read()
try:
data = data.decode("utf-8")
# If data can't be decoded, then it doesn't have that attribute.
# Catch that exception
except AttributeError:
pass
# Python 3 has a string method called .splitlines() that makes
# splitting lines a little simpler.
myList = [line for line in data.splitlines()]
# We'll return two values: The length (or count) of the list
# and the actual list.
return len(myList), myList
# We set up two variables for intake of our count and list results.
count, myList = FileOpen()
print("{} number of lines read from file".format(count))
EDIT: I don't know what the last part of your code is aiming to do. Maybe compare student count to valid entries? Here's the middle part. I moved the function outside and made it evaluate either valid or invalid in one fell swoop.
def is_valid(entry_value):
"""Validate current entry."""
if len(entry_value) != 8 and entry_value.isnumeric():
return False
return True
def AnalyseStudents(rawStudents, totalStudents):
for entry in rawStudents:
# Our new function will return True if the entry is valid
# So we can rework our function to run through the True condition
# first.
if is_valid(entry):
entry_sum = entry[0] * 8
entry_sum += entry[1] * 7
entry_sum += entry[2] * 6
entry_sum += entry[3] * 5
entry_sum += entry[4] * 4
entry_sum += entry[5] * 3
entry_sum += entry[6] * 2
entry_sum += entry[7] * 1
if entry_sum % 11 == 0:
print(entry, " is an VALID student number")
else:
print(entry, " is an INVALID student number")
else:
print(entry, " is an INVALID student number")
AnalyseStudents(inputList, outputList)

Python Sudoku Checker 9X9

while True:
try:
file = input("Enter a filename: ")
fi = open(file, "r")
infile = fi.read()
grid = [list (i) for i in infile.split()] #Puts the sudoku puzzle into a list in order to check that the total number is valid
check = len(grid)
print("The total number in this puzzle is:",check) #Counts the amount of numbers in the sudoku puzzle
break
except FileNotFoundError:
print ("The inputted file does not exist")
def check(infile):
count = 0
for j in range (0,9):
for n in range(0,9):
if infile[j].count(infile[j][n]) <= 1:
count = count + 0
else:
count = count + 1
cols = [[row[i] for row in infile] for i in[0,1,2,3,4,5,6,7,8]]
leg = 0
for i in range(0,9):
for j in range(0,9):
if cols[i].count(cols[i][j]) <= 1:
leg = leg + 0
else:
leg = leg + 1
angel = []
for t in range(3):
ang = infile[t]
for u in range(3):
angel.append(ang[u])
foot = 0
for be in range(9):
if angel.count(angel[be]) <= 1:
foot = foot + 0
else:
foot = foot + 1
if count + leg + foot == 0:
print("Valid")
else:
print ("Invalid")
def inputs():
x = raw_input()
ls = []
while x != '':
x1 =x.split(' ')
ls.append(x1)
if len(infile) >=9:
print (check(infile))
infile = []
x = raw_input()
inputs()
actual error:
Traceback (most recent call last):
File "E:/Computer Programming/Assignment/check 2.py", line 22, in <module>
cols = [[row[i] for row in infile] for i in[0,1,2,3,4,5,6,7,8]]
File "E:/Computer Programming/Assignment/check 2.py", line 22, in <listcomp>
cols = [[row[i] for row in infile] for i in[0,1,2,3,4,5,6,7,8]]
File "E:/Computer Programming/Assignment/check 2.py", line 22, in <listcomp>
cols = [[row[i] for row in infile] for i in[0,1,2,3,4,5,6,7,8]]
IndexError: string index out of range
Why does it give an output to say that my string index is out of range, is there another way to create a sudoku 9x9 checker to check if there are any reoccurring numbers. I need to make sure that there are 9 numbers in each column and that they are between the numbers 1 and 9
first, a few comments:
never do:
cols = [[row[i] for row in infile] for i in[0,1,2,3,4,5,6,7,8]]
but do:
cols = [[row[i] for row in infile] for i in range(0,9)]
never call a variable the same name as a function you've defined in your code check and check()
don't write code at the module level, but embed everything in functions, and call the entry point function at the end of the file after the if __name__ == "__main__" condition (so in case you want to import your module in another module, you don't execute module level code).
don't open files without closing them, instead use the context manager: with open('myfile', 'r') as f: ...
your code features an useless use of while... or at least a wrong use (do you really mean to loop forever on an exception?) use command line arguments instead, that will make the shell help your user choose a file that does actually exists.
now I've made all that clear, here's about your actual question:
infile is a file object (if I can read correctly your mis-indented python code), thus every line - called row here - of infile is just a string.
So if you have an empty line or a line that is less than 9 columns, you're likely to get row[i] out of boundaries.
here's a take at refactoring your code, though I've left a number of wrong design over:
def check(infile):
count = 0
for j in range (0,9):
for n in range(0,9):
if infile[j].count(infile[j][n]) <= 1:
count = count + 0
else:
count = count + 1
def inputs():
x = raw_input()
ls = []
while x != '':
x1 =x.split(' ')
ls.append(x1)
if len(infile) >=9:
print (check(infile))
infile = []
x = raw_input()
def check_grid():
cols = [[row[i] for row in infile] for i in range(0,9)]
leg = 0
for i in range(0,9):
for j in range(0,9):
if cols[i].count(cols[i][j]) <= 1:
leg = leg + 0
else:
leg = leg + 1
angel = []
for t in range(3):
ang = infile[t]
for u in range(3):
angel.append(ang[u])
foot = 0
for be in range(9):
if angel.count(angel[be]) <= 1:
foot = foot + 0
else:
foot = foot + 1
if count + leg + foot == 0:
print("Valid")
else:
print ("Invalid")
inputs()
def sudoku_checker():
try:
file = input("Enter a filename: ")
fi = open(file, "r")
infile = fi.read()
grid = [list (i) for i in infile.split()] #Puts the sudoku puzzle into a list in order to check that the total number is valid
# Counts the amount of numbers in the sudoku puzzle
print("The total number in this puzzle is:",len(grid))
check_grid()
except FileNotFoundError:
print ("The inputted file does not exist")
if __name__ == "__main__":
sudoku_checker()

Categories