Using my current setup on my code; how do i output only the False objects ("hasBeenRead = False")
#An SMS Simulation
SMSStore = []
unreadMessage = []
class SMSMessage(object):
def __init__(self, messageText, fromNumber):
self.hasBeenRead = False
self.messageText = messageText
self.fromNumber = fromNumber
def markAsRead(self, hasBeenRead):
hasBeenRead = True
def add_sms(self):
newMessage = (self.hasBeenRead, self.messageText, self.fromNumber)
return SMSStore.append(newMessage)
def get_count(self):
return len(SMSStore)
def get_message(self, q):
print (SMSStore[q][1])
self.get_hasBeenRead()
def get_hasBeenRead(self):
self.hasBeenRead = True
def get_unread_messages(SMSStore):
counter = 0
if SMSStore[counter][0] == False:
print "From: " + SMSStore[counter][2]
print "Message: " + SMSStore[counter][1]
counter = counter +1
def remove(self, i):
return SMSStore.remove(i)
#sample = SMSMessage("Hello friend!", 0742017560)
userChoice = ""
while userChoice != "quit":
userChoice = raw_input("What would you like to do - read/send/quit?")
if userChoice == "read":
i = int((raw_input("Please enter which messsage number you want to read: ")))
messageText = "null"
fromNumber = "null"
newObject1 = SMSMessage(messageText, fromNumber)
newObject1.get_message(i)
readUnread = raw_input("Would you like to read the unread messages: yes/no?")
if readUnread == "yes":
newObject1.get_unread_messages()
else:
print "All messages have been read"
elif userChoice == "send":
messageText = raw_input("Please type in your message: ")
fromNumber = raw_input("Please type in the number it was sent from ")
newObject = SMSMessage(messageText, fromNumber)
newObject.add_sms()
print str(newObject.get_count()) + " is now the new length of the list"
elif userChoice == "quit":
print "Goodbye"
else:
print "Oops - incorrect input"
As you can see i am quiet a beginner at coding but using what i have setup is it possible to only output objects from the list that have False?
Related
def market_list(user_request):
fixed_list_option_1 = customer_products_string.split(' ', )
if user_request == 1:
return fixed_list_option_1
elif user_request == 2:
return fixed_list_option_1.count
customer_products_string = "pizza,apple"
user_request = input("What is your request? (insert the number of the command) : ")
print(market_list(user_request))
Input seems string here. That's why you should use user_request == "1" or "2" or int(input("...")).
def market_list(user_request):
fixed_list_option_1 = customer_products_string.split(',', )
if user_request == "1":
return fixed_list_option_1
elif user_request == "2":
return len(fixed_list_option_1)
customer_products_string = "pizza,apple"
user_request = input("What is your request? (insert the number of the command) : ")
print(market_list(user_request))
My code prints out every time 'Invalid input', but I want it to print 'Invalid input' only when something other besides "rock", 'paper' , 'scissors', '!exit', '!rating' is written in the input, can someone help me, please, I don't really get why it is like this. Here you can see my code:
import random
import math
HELP_TEXT = '"'
class Rating:
def __init__(self, name):
self._old_data = None
self._user_name = name
self.score = self._get()
def _get(self):
try:
with open('rating.txt', 'r') as f:
for line in f:
name, score = line.split()
if name == self._user_name:
self._old_data = line
return int(score)
with open('rating.txt', 'a') as f:
f.write(f'{self._user_name} 0')
self._old_data = f'{self._user_name} 0'
return 0
except FileNotFoundError:
with open('rating.txt', 'w') as f:
f.write(f'{self._user_name} 0')
self._old_data = f'{self._user_name} 0'
return 0
def add(self, score):
self.score += score
def save(self):
with open('rating.txt', 'r') as f:
old_data = f.read()
new_data = old_data.replace(self._old_data, f'{self._user_name} {self.score}\n')
with open('rating.txt', 'w') as f:
f.write(new_data)
class User:
def __init__(self):
self.name = ''
self._hello_user()
self.rating = Rating(self.name)
def _hello_user(self):
self.name = input('Enter your name: ').strip().replace(' ', '')
print(f'Hello, {self.name}')
class Game:
def __init__(self):
self.RPS = {}
self.user_choice = ''
self.game_choice = ''
self.result = ''
self.help = HELP_TEXT
self.user = User()
def referee(self):
if self.user_choice == self.game_choice:
return 'draw'
elif self.user_choice in self.RPS.get(self.game_choice):
return 'win'
else:
return 'lose'
def result_processing(self):
if self.result == 'draw':
self.user.rating.add(50)
print(f'There is a draw ({self.game_choice})')
elif self.result == 'lose':
print(f'Sorry, but computer chose {self.game_choice}')
elif self.result == 'win':
self.user.rating.add(100)
print(f'Well done. Computer chose {self.game_choice} and failed')
else :
print('Invalid input')
def generator(self, user_input):
output = {}
items = list(user_input.split(','))
if len(items) == 1:
return {'rock': ['paper'], 'paper': ['scissors'], 'scissors': ['rock']}
double_items = items + items
half_items = math.ceil(len(items) / 2)
for i in range(len(items)):
output[items[i]] = double_items[i + 1:i + half_items:1]
return output
def run(self):
self.RPS = self.generator(input())
print('Okay, let\'s start')
while True:
self.user_choice = input().strip()
if self.user_choice == '!exit':
break
elif self.user_choice == '!rating':
print(f'Your rating: {self.user.rating.score}')
continue
elif self.user_choice == '!help':
print(self.help)
continue
self.game_choice = random.choice(list(self.RPS.keys()))
self.result = self.referee()
self.result_processing()
self.user.rating.save()
print('Bye!')
if __name__ == '__main__':
game = Game()
game.run()
You can add a check that the input is one of the keys to referee().
def referee(self):
if self.user_choice == self.game_choice:
return 'draw'
elif self.user_choice not in self.RPS:
return 'invalid'
elif self.user_choice in self.RPS[self.game_choice]:
return 'win'
else:
return 'lose'
I have an employee class which has an init method that reads an employees record in from a file. I also have an employees class which has an init_ method that attempts to read in all of the employee records.
I am having trouble with init for the employees class. Here is my code:
class EmployeeList():
records=[]
def __init__(self):
with open(database) as fp:
emp=employee(fp)
while (emp.id > 0):
print(emp)
self.records.append(emp)
emp=employee(fp)
The print(emp) is there for error checking, it shows that the records are being read in properly. When the EOF is reached, the init method for the employee sets the id to 0 and the name of the employee to "". I have two problems:
After the loop, all of the employees in employees.records are the same - id 0 and blanks for names. I am assuming that that emp is not creating a new instance each time it is called, and so all of the employees are being set to that one instance of emp, the very last one from when EOF is reached.
I doubt my code is "Pythonesque"; suggestions for improvement are welcome.
P.S. database is globally defined to the file name.
The entire code is here, sorry about the length:
class employee:
count = 0
def __init__(self,f=None):
if (f==None): # a user is inputting the employee
self.lastname = input("Employees last name:")
while type(self.lastname)!=type("1"):
print("Your input needs to be a name\n")
self.lastname = input("Employees last name:")
self.firstname = input("Employees first name:")
while type(self.firstname)!=type("1"):
print("Your input needs to be a name\n")
self.firstname = input("Employees first name:")
self.payrate = float(input("Employees pay rate:"))
while type(self.payrate)!=type(0.0):
print("Your input needs to be a pay rate\n")
self.payrate = float(input("Employees pay rate:"))
employee.count = employee.count + 1
self.id = employee.count
else: # the employee is being read in from the database and f is a file pointer
# read in an employee record and return false for end of file.
checkEOF = f.readline().rstrip('\r\n') #check for end of file
if (checkEOF != ""):
employee.id = int(checkEOF)
employee.firstname = f.readline().rstrip('\r\n')
employee.lastname = f.readline().rstrip('\r\n')
employee.payrate = float(f.readline().rstrip('\r\n'))
else:
employee.id = 0
employee.firstname = " "
employee.lastname = " "
employee.payrate = 0.0
def __hash__(self):
return hash(self.id)
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.id == other.id
return NotImplemented
def __str__(self):
return "Employee " + str(self.id) + " is "+self.firstname + " "+self.lastname+" and their pay rate is "+str(self.payrate)+"\n"
def __lt__(self, other):
if (self.lastname < other.lastname):
return True
elif (self.lastname > other.lastname):
return False
else: #same last names
if (self.firstname < other.firstname):
return True
elif (self.firstname > other.firstname):
return False
else: #same names
if (self.id < other.id):
return True
else: # note that ids cannot be the same
return False
def __gt__(self, other):
if (self.lastname > other.lastname):
return True
elif (self.lastname < other.lastname):
return False
else: # Same last names
if (self.firstname > other.firstname):
return True
elif (self.firstname > other.firstname):
return False
else: # Same names
if (self.id > other.id):
return True
else: # note that ids cannot be the same
return False
def payraise(self,payraise):
self.payrate = self.payrate+payraise
def saveemployee(self,fp):
fp.write(str(self.id)+"\n")
fp.write(self.firstname+"\n")
fp.write(self.lastname+"\n")
fp.write(str(self.payrate)+"\n")
class EmployeeList():
records=[]
def __init__(self):
with open(database) as fp:
emp=employee(fp)
while (emp.id > 0):
print(emp)
self.records.append(emp)
emp=employee(fp)
def __str__(self):
employeesprint = ""
for emp in self.records:
employeesprint = employeesprint + str(emp)
return employeesprint
def save(self):
self.records.sort()
with open(database,"w+") as fp:
fp.seek(0)
for emp in self.records:
emp.saveemployee(fp)
def menu():
print("\n")
print(choices[0]+". Add another employee")
print(choices[1]+". Print employees")
print(choices[len(choices)-1]+". Quit")
print("\n")
employees = EmployeeList()
choices = ["A","B","C"]
ch = "N"
while (ch != choices[len(choices)-1]):
menu()
ch=input("Make your choice ")
while not (ch in choices):
menu()
ch=input("Make your choice ")
if (ch == choices[0]):
employees.records.append(employee())
employees.save()
if (ch == choices[1]):
print(employees)
Sample output: You can see the two employees correctly being printed as they are read in:
Employee 1 is jane bob and their pay rate is 1.0
Employee 2 is jim bob and their pay rate is 3.4
A. Add another employee
B. Print employees
C. Quit
Make your choice B
Employee 0 is and their pay rate is 0.0
Employee 0 is and their pay rate is 0.0
your code:
if (checkEOF != ""):
employee.id = int(checkEOF)
employee.firstname = f.readline().rstrip('\r\n')
employee.lastname = f.readline().rstrip('\r\n')
employee.payrate = float(f.readline().rstrip('\r\n'))
else:
employee.id = 0
employee.firstname = " "
employee.lastname = " "
employee.payrate = 0.0
change 'employee' to 'self', 'employee' is the name of the class.
Why the menu does not start (solved by Roman Susi)
Why the menu do not work as expected (error below)
How do I solve the error in my foo.add code?
Traceback (most recent call last):
File "C:\Users\User\Desktop\phonedatabase.py", line 81, in <module>
openphonedb()
File "C:\Users\User\Desktop\phonedatabase.py", line 23, in openphonedb
for entry in foo.add(name, number, showtype):
TypeError: 'NoneType' object is not iterable
This error happens when adding a new user, after typing in the 'Type'
import shelve
import string
UNKNOWN = 0
HOME = 1
WORK = 2
FAX = 3
CELL = 4
def openphonedb():
foo = phonedb()
print "What would you like to do?"
print "Add = 1, Lookup = 2, Exit = 3"
entry = int(raw_input('>> '))
if entry == 1 :
namelookup = raw_input('Please enter a name: ')
for entry in foo.lookup(namelookup):
print '%-40s %s (%s)' % (entry.name, entry.number, entry.showtype() )
elif entry == 2:
name = raw_input('Name: ')
number = raw_input('Number: ')
showtype = input('Type (UNKNOWN, HOME, WORK, FAX, CELL): \n>> ')
for entry in foo.add(name, number, showtype):
print '%-40s %s (%s)'% (entry.name, entry.number, entry.showtype() )
elif entry == 3:
print "Close Successful"
quit
else:
print "Invalid."
openphonedb()
class phoneentry:
def __init__(self, name = 'Unknown', number = 'Unknown',
type = UNKNOWN):
self.name = name
self.number = number
self.type = type
# create string representation
def __repr__(self):
return('%s:%d' % ( self.name, self.type ))
# fuzzy compare or two items
def __cmp__(self, that):
this = string.lower(str(self))
that = string.lower(that)
if string.find(this, that) >= 0:
return(0)
return(cmp(this, that))
def showtype(self):
if self.type == UNKNOWN: return('Unknown')
if self.type == HOME: return('Home')
if self.type == WORK: return('Work')
if self.type == FAX: return('Fax')
if self.type == CELL: return('Cellular')
class phonedb:
def __init__(self, dbname = 'phonedata'):
self.dbname = dbname;
self.shelve = shelve.open(self.dbname);
def __del__(self):
self.shelve.close()
self.shelve = None
def add(self, name, number, type = HOME):
e = phoneentry(name, number, type)
self.shelve[str(e)] = e
def lookup(self, string):
list = []
for key in self.shelve.keys():
e = self.shelve[key]
if cmp(e, string) == 0:
list.append(e)
return(list)
#edit
if __name__ == '__main__':
openphonedb()
Maybe, you just forgot to call the openphonedb()?
Add to the end:
if __name__ == '__main__':
openphonedb()
Also, what "quit" is doing there?
code has other issues, but with regard to the first part of the question, what about comparing entry as a string (so if an user types "a" you don't get an error)?
def openphonedb():
foo = phonedb()
print "What would you like to do?"
print "Add = 1, Lookup = 2, Exit = 3"
while True: # note this
entry = raw_input('>> ') # removed int()
if entry == '1' :
namelookup = raw_input('Please enter a name: ')
for entry in foo.lookup(namelookup):
print '%-40s %s (%s)' % (entry.name, entry.number, entry.showtype() )
elif entry == '2':
name = raw_input('Name: ')
number = raw_input('Number: ')
showtype = input('Type (UNKNOWN, HOME, WORK, FAX, CELL): \n>> ')
for entry in foo.add(name, number, showtype):
print '%-40s %s (%s)'% (entry.name, entry.number, entry.showtype() )
elif entry == '3':
print "Close Successful"
exit() # note this also
else:
print "Invalid."
import pickle
class TasksError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class Task(object):
def __init__(self, task = () ):
if task ==():
raise TasksError('Empty task.')
self.name = task[0]
self.date = task[1]
self.priority = task[2]
self.time = task[3]
self.type = task[4]
self.comment = task[5]
def __str__(self):
output = '''Name: %s
Date: %s
Priority: %s
Time: %s
Type: %s
Comment: %s
''' % ( self.name,
self.date,
self.priority,
self.time,
self.type,
self.comment)
return output
class Tasks(object):
def __init__(self, container = []):
self.container = [ Task(todo) for todo in container ]
def delete(self):
x = 0
for todo in self.container:
x = x + 1
print "Task Number",x,"\n", todo
delete = raw_input("what number task would you like to delete")
if delete == "y":
del todo
############
#x = 0
# for task in self.container:
# x = x+1
#print "Task Number",x,"\n", task
#delete = raw_input("what number task would you like to delete")
#if delete == "y":
#del(task)
def add(self, task):
if task == '':
raise TasksError('Empty task')
self.container.append( Task(task) )
def __str__(self):
output = '\n'.join( [ str(todo) for todo in self.container ] )
return output
if __name__== "__main__":
divider = '-' * 30 + '\n'
tasks = Tasks( container = [] ) # creates a new, empty task list
while True:
print divider, '''Make your selection:
1. Add new task
2. Print all tasks
3. Save tasks
4. Load tasks from disk
5. Find high priority tasks
6. Sort by date
7. Delete task
<ENTER> to quit
'''
try:
menu_choice = int(input("Select a number from the menu: "))
except:
print 'Goodbye!'
break
if menu_choice == 1:
task = raw_input (">>> Task: ")
date = raw_input (">>> Date as string YYYYMMDD: ")
priority = raw_input (">>> Priority: ")
time = raw_input (">>> Time: ")
Type = raw_input (">>> Type Of Task: ")
comment = raw_input (">>> Any Comments? ")
todo = (task, date, priority, time, Type, comment)
tasks.add( todo )
print tasks
elif menu_choice == 2:
print divider, 'Printing all tasks'
print tasks
elif menu_choice == 3:
print divider, 'Saving all tasks'
tasks.save()
elif menu_choice == 4:
print divider, 'Loading tasks from disk'
tasks.load()
elif menu_choice == 5:
print divider, 'Finding tasks by priority'
results = tasks.find_by_priority(priority='high')
for result in results: print result
elif menu_choice == 6:
print divider, 'Sorting by date'
tasks.sort_by_date()
print tasks
elif menu_choice == 7:
tasks.delete()
I have deleted parts of my code (hopefully nothing important).
Im having trouble getting python to delete my tasks once added.
Both methods defined as "def delete" give the error message type error: task/todo object does not support deletion.
Does anyone know a way around this?
You don't delete from list like that... Your code have 2 problems:
if you use for to loop through a iterable, you should not change it inside the loop.
to del from list you should use index.
Try this:
index = 0
while index < len(self.container):
delete = raw_input("what number task would you like to delete")
if delete == "y":
del self.container[index]
else:
index += 1