#๐ little hw help
34 messages ยท Page 1 of 1 (latest)
@inner forum
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
alright so
name = name.strip()
id_num = id_num.rstrip()
is .rstrip() misspelled?
are the subsequent .strip() meant to be spelt like that?
This is just lecture code I'm using as reference for my assignment
It's not a misspelling .rstrip and .lstrip are valid methods in Python. They stand for right strip and left strip meaning. rstrip will remove trailing characters, while lstrip removes leading characers. and strip removing left and right characters.
'characters' in this case meaning space-like, invisible characters, like spaces, tab spaces and line breaks - that's what stripping without arguments will take care of
`def main():
f = open('records.txt', 'r')
search_name = input('Enter the name of the person whose record you want to delete: ')
name = f.readline()
found = False
while name!='':
age = f.readline()
dept = f.readline()
name = name.strip()
age = age.strip()
dept = dept.strip()
if name == search_name:
found = True
del name
del age
del dept
print('Records for ', name ,'deleted succesfully.')
print(f.readlines())
break
name = f.readline()
f.close()
if not found:
print('Not found.')
main()`
The records file:
Bob 25 Software
Charlie 40 Trade
Dennis 19 Business
Ashley 39 HR
Why isnt it working?
I input Bob
and get "Not found"
Why are you using del?
This should be output
Delete the record
wait
I only opened it to read
I wouldnt be able to modify it anyway
Should I try making a temp file then
uh
`y = {}
eliminate the unrequired element
for key, value in test_dict.items():
if key != 'Arushi':
y[key] = value
print(y)`
This works for a dictionary
can I do something similar for a record?
inputfile = 'records.txt' with open(inputfile, 'r') as file: inputfilelines = file.readlines() lineindex = 1 deleteline = input('Enter the name of the person whose record you want to delete: ') with open(inputfile, 'w') as file: for textline in inputfilelines: if lineindex != deleteline: file.write(textline) lineindex += 1 print('Record for ',deleteline,'deleted successfully.') print('Remaining records:') givenfile = open(inputfile, 'r') for line in givenfile: print(line) file.close()
It doesnt work
because permission denied
sorry i was gonna close this
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.
๐ little hw help