Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Need a little Help with my Script

I'm going over some of the some of the thing I've learned over the past year as a sort of refresher and today I've been writing a script that checks a text file for a Number and if the file contains the number it outputs:

'Duplicate, this Number already in List'

And if the number isn't in the List it appends it to the List

I've been working on it all day and at first it was working fine but then I started to play around with the code and now it appends everything to the list even if it's already n the list

I've made a post on paste bin here:

https://pastebin.com/d9gcGuXs

Thanks the Duke
[Image: Web-Logo-copy.gif]
(Oct 5, 2021 12:50 PM)ThinWhiteDuke Wrote:  I'm going over some of the some of the thing I've learned over the past year as a sort of refresher and today I've been writing a script that checks a text file for a Number and if the file contains the number it outputs:

'Duplicate, this Number already in List'

And if the number isn't in the List it appends it to the List

I've been working on it all day and at first it was working fine but then I started to play around with the code and now it appends everything to the list even if it's already n the list

I've made a post on paste bin here:

https://pastebin.com/d9gcGuXs

Thanks the Duke

I didn't test it but this should work.
Code
import json
print('Enter your number:', end=" ")
x = input()
with open('numbers.json', 'r+') as json_file:
    data = json.load(json_file)
    d = data
    if x in data:
        print('Duplicate, this Number already in List')
    else:
        d.append(x)
        json_file.seek(0)
        json.dump(d, json_file)

Make sure you have the json file or check for it first ✌
echo "[]" >> numbers.json

//edit: It looks like you had an indentation error on else. Also, you can't use if x in ListA as that's just the variable to the absolute path of the file. In this case, it would look if the number is in the path which would always be false. You need to read the file first, then look for the content.
Microsoft Word is the best IDE for programming
(Oct 5, 2021 01:28 PM)step1step2 Wrote:  I didn't test it but this should work.
Code
import json
print('Enter your number:', end=" ")
x = input()
with open('numbers.json', 'r+') as json_file:
    data = json.load(json_file)
    d = data
    if x in data:
        print('Duplicate, this Number already in List')
    else:
        d.append(x)
        json_file.seek(0)
        json.dump(d, json_file)

Make sure you have the json file or check for it first ✌
echo "[]" >> numbers.json

First of all Thanks very much for your help, I’ve been playing around with json trying a few things , the code you wrote works fine.
Just need to ask you the line that says: json_file.seek(0) what does the .seek() do as I’ve tried commenting it out and the code still runs fine
[Image: Web-Logo-copy.gif]
(Oct 6, 2021 09:25 AM)ThinWhiteDuke Wrote:  First of all Thanks very much for your help, I’ve been playing around with json trying a few things , the code you wrote works fine.
Just need to ask you the line that says: json_file.seek(0) what does the .seek() do as I’ve tried commenting it out and the code still runs fine

seek() in Python just resets the current position to the offset. It defaults to zero.

I think it would be used if you wanted to read from the file again from the beginning, or from elsewhere within the file. There is an example of it on this site.

https://www.tutorialspoint.com/python/file_seek.htm
[Image: Iu0EfTL.gif]
(Oct 6, 2021 02:43 PM)Lindros Wrote:  seek() in Python just resets the current position to the offset. It defaults to zero.

I think it would be used if you wanted to read from the file again from the beginning, or from elsewhere within the file. There is an example of it on this site.

https://www.tutorialspoint.com/python/file_seek.htm

Thanks for taking the time to reply
[Image: Web-Logo-copy.gif]