Author Topic: New Problem in Python  (Read 1653 times)

The last problem was fixed, this is a new problem.

So, My last problem was resolved, now I have a new issue.

I cant seem to find it online, either that or I'm somehow not looking hard enough.

I would like to know if its possible in Python to make the program log information of something you put in a text file somewhere.

eg.

Quote
username = input ("What is your username?: ")
print ("Your username is now " + username)
import time
time.sleep(1)
quit()

And just before that quit command, a text file is created with the username you selected.

Any help?

« Last Edit: September 10, 2013, 03:22:34 PM by Kirze »

I don't know much about python but if it's similar to java or c++ that's not how you make an if statement.

it would be like this

Code: [Select]
choice = input("Want to play a game? Yes/No: )
if(choice == "yes")

     print("Yes")

else

     print("You chose No")


It seems like you need to read a little bit more about python. If statements are like the most basic parts of a language. Try following some tutorials online going over it.

I don't know much about python but if it's similar to java or c++ that's not how you make an if statement.

it would be like this

Code: [Select]
choice = input("Want to play a game? Yes/No: )
if(choice == "yes")

     print("Yes")

else

     print("You chose No")


It seems like you need to read a little bit more about python. If statements are like the most basic parts of a language. Try following some tutorials online going over it.

Thank you. So much.


Code: [Select]
import pickle

#to read from file
pkl_file1 = open('readfile.pkl', 'rb') #Opens the file in read mode.
readback = pickle.load(pkl_file1) #This assigns whatever is in the file to the variable. If you have a dictionary, you can use eval.
pkl_file1.close()
#to write to file
output = open('writefile.pkl', 'wb') #Opens the file in write mode.
pickle.dump(username, output) #Dumps the variable "username" into the output which is the writefile.pkl opened in write mode.
output.close() #Closes the file.
.pkl file can be opened with notepad, I'm not sure if it also works with .txt instead of .pkl but that's the way I did it before.

If I'm wrong someone can correct me.

Edit: The write/read code also creates the file if it does not exist.
Edit2: Yeah it works with .txt files instead of .pkl. Tested it.
« Last Edit: September 10, 2013, 03:37:30 PM by Blockzillahead »

Ok, so if I am reading it right, you want code in python so you enter in a name and it stores it into a txt file for later use:

Code: [Select]
import os.path
if os.path.exists("storage.txt"): # Check if the file "storage.txt" exists,

    a = open("storage.txt", "r+") # Open the file storage.txt, we know it exists because of the previous line ^
    hi = a.read(-1); # Change -1 to how many letters to read, -1 means everything

    print ("Hi, your username is - "+ str(hi)) # Show your name,
    changename = input("Would you like to change your name? - ").lower() # Ask to change name, .lower means make it all lowercase.
   
    if changename == "yes":
        username = input("Hello, enter in your name please - ")
        print ("Your username is now - "+ str(username))

        a = open("storage.txt", "w") # Open storage.txt for writing in the new username
        a.write(username) # Write the new username to the file.
        a.close() # Close the file.

    else:
        print ("Bye,")

else:
    username = input("Hello, enter in your name please - ") # Enter a string for the variable username
   
    a = open("Storage.txt", "w") # Check if a file with that name exists, if not creates the file. "W" means write
    a.write(username) # Puts in storage.txt what the variable username is.
    a.close() # Close file
   
    print ("Your username is -", username) # Print username




This is python 3.3 also.
« Last Edit: September 11, 2013, 01:11:55 AM by Starzy »

-Snip-

completely different. ignore this
« Last Edit: September 11, 2013, 01:12:27 AM by Starzy »

Op, did it work?
Or wasn't it what you wanted?