0

Can someone explain why when I run this it asks for input 2 times when I say to create another save file? I'm using python 3.9.1 on a mac with default python IDLE.

Here is my code:

import pickle
names = []
count = 0
times = 1
a = "false"
gameplayfunc = "false"
while (count < 9):
    for i in range(times):
        names.append(input('Please enter save file name: '))
    x = input("Would you like to create another save file?")
    if x == "Yes" or x == "yes" or x == "YES":
        names[i]
        pickle.dump( x, open( names[i], "wb" ) )
        times = times + 1
        a == "true"
    elif x == "No" or x == "no" or x == "NO":
        namee = names[i] + json
        pickle.dump( x, open( namee, "wb" ) )
        gameplayfunc = "true"
        count = 10
while a == "true":
    count = 10
    count = 1
    a == "false"
    
...

for i in range(times):
    print(names[i])
SiHa
  • 7,830
  • 13
  • 34
  • 43
rcode
  • 3
  • 1

1 Answers1

0
import pickle
names = []
count = 0
times = 1
a = "false"
gameplayfunc = "false"
while (count < 9):
    for i in range(times):
        names.append(input('Please enter save file name: '))
    x = input("Would you like to create another save file?")
    if x == "Yes" or x == "yes" or x == "YES":
        names[i]
        pickle.dump( x, open( names[i], "wb" ) )
        times = times + 1 # Times is incremented!!!!
        a == "true"
    elif x == "No" or x == "no" or x == "NO":
        namee = names[i] + json
        pickle.dump( x, open( namee, "wb" ) )
        gameplayfunc = "true"
        count = 10

Notice in your code, you are asking the user to enter a file name for i in range(times). If the user says "yes", you are incrementing times, and thus the first time you ask for the file name, times is 2, which is why it asks twice. If you say yes a second time, it will ask for three file names.

Note also that opened files should be closed, you should instead write something like

dump_file = open( names[i], "wb" )
pickle.dump( x, dump_file )
dump_file.close() 

Also note that True and False exist in python, which allows you to write if a instead of if a == True if you assign a = True and a = False.

Kraigolas
  • 5,121
  • 3
  • 12
  • 37
  • I know about the true false thing but it does not seem to be working so I just used strings ¯\_(ツ)_/ – rcode Feb 19 '21 at 12:34
  • @rcode Note that `True` and `False` are case sensitive. If my answer answers your question please accept it or please comment the problem with the answer. – Kraigolas Feb 19 '21 at 12:43