1

I need to make a code that lets the user input five numbers and will sort it from greatest to least. They can also choose if he wants do delete, add, or change a variable and make, save, or open a list. I do not know how to make the saving, opening and making part, so I deleted it, please help me.

l=1
print ("Please input five numbers")
a=int(input("1.:"))
print (a, "is the first number.")
print()
b=int(input("2.:"))
print (b, "is the second number.")
print()
c=int(input("3.:"))
print (c, "is the third number.")
print()
d=int(input("4.:"))
print (d, "is the fourth number.")
print()
e=int(input("5.:"))
print (e, "is the fifth number.")
print()

x=[a, b, c, d, e]
y=sorted(x,reverse=True)

print (y)

while l==1:
        print()
        print ("If you want to delete a number, press D.")
        print()
        print ("If you want to add a number, press A.")
        print()
        print ("If you want to change a number, press C.")
        print ()
        print ("If you want to exit, press Q.")
        print ()
        z=input("Answer: ")

        if z=="A":
                f=int(input("Please input another number: "))
                x.append(f)
                y=sorted(x,reverse=True)
                print (y)

        elif z=="C":
                g=int(input("Input a number you will change: "))
                if g in x:
                    x.remove(g)
                    print (g, "is removed.")
                    f=int(input("Put the number you want to replace: "))
                    x.append(f)
                    y=sorted(x,reverse=True)
                    print (y)

                elif g not in x:
                    print ()
                    print (g, "is not in the list.")
                    y=sorted(x, reverse=True)
                    print (y)

        elif z=="D":
                g=int(input("Input a number you will delete: "))
                if g in x:
                    x.remove(g)
                    print (g, "is removed.")
                    y=sorted(x,reverse=True)
                    print (y)
                elif g not in x:
                    print ()
                    print (g, "is not in the list.")
                    y=sorted(x, reverse=True)
                    print (y)
        elif z=="Q":
                import sys
                print ("Thanks!")
                sys.exit
                break
        else:   
            print ("Sorry. I could not understand. Try again.")```
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Domzymonz
  • 13
  • 1
  • 6
  • 4
    Save the list in a file. You can use `json` to format and parse it. – Barmar Mar 12 '20 at 08:04
  • 1
    You can also "save" it (the correct term is "serialize") using pickle (https://docs.python.org/3/library/pickle.html). This can be used for a variety of python objects such as dictionaries etc... – ma3oun Mar 12 '20 at 08:11
  • Might be helpful (and a duplicate...): https://stackoverflow.com/questions/14509269/best-method-of-saving-data or https://stackoverflow.com/questions/1047318/easiest-way-to-persist-a-data-structure-to-a-file-in-python – Tomerikoo Mar 12 '20 at 08:13

2 Answers2

0

You can save this to a file or even use the shelve module.

import shelve

s = shelve.open('test.db')
    s['key'] = [1, 2, 34, 5, 33]

This creates a file storing the data.

To retrieve the list.

import shelve


r = shelve.open('test.db')
print (r['key'])
r.close()

This returns the original list. The advantage of this method is the data type is preserved.

CodeCupboard
  • 1,507
  • 3
  • 17
  • 26
0

You can open a file and save/load your list as string

    import os
    import ast

    my_list = [1,2,3,4,5]
    save_file = 'save_file.txt.'

    # save list in file
    with open(save_file, 'w') as f:
        f.write(str(my_list))

    # load list from file
    if os.path.exists(save_file):
        with open(save_file) as f:
             my_list = ast.literal_eval(f.read())
dsgou
  • 129
  • 2
  • 7