How to override lists? - python

I have a question. I'm programming the gallows game. Everything's fine, but I'm stuck with something. The user selects a letter, which will be displayed on the screen and in the corresponding box. But every time the user selects a letter, the, Those I used to choose are gone.
You can see:
import pyfiglet
import random
import os
def start():
print(pyfiglet.figlet_format("BIENVENIDO AL JUEGO DEL AHORCADO"))
print("""
ยก A D I V I N A L A P A L A B R A !
""")
def game():
with open("./archivos/data.txt", "r", encoding="utf-8") as f:
palabras = list(f)
palabra = random.choice(palabras)
palabra = palabra.replace("\n", "")
letras = [i for i in palabra]
guiones = []
for i in letras:
guiones.append("_")
print(" ".join(guiones))
print("")
guiones = []
while guiones != letras:
letra = input("Elige una letra: ")
for i in palabra:
if i == letra:
guiones.append(letra)
else:
guiones.append("_")
print(" ".join(guiones))
guiones.clear()
def run():
start()
game()
if __name__ == "__main__":
run()

Since You are working with lists (which are mutable), You can flip the "_" to the guessed letter and not touch the rest of guiones. To do so, one solution is to use an index to access the list at the given position, like in the code snippet below :
# guiones = [] do NOT clear guiones!
while guiones != letras:
letra = input("Elige una letra: ")
for i in range(len(palabra)):
if palabra[i] == letra:
guiones[i] =letra
print(" ".join(guiones))

Related

Checking if a list has something in it

I'm currently making a "match stick" game in CLI and It's Player vs AI. Almost everything works properly except that the "AI" chooses a stick that has been previously removed.
Here is the code:
class CPyramide(object):
def __init__(self, lines):
self.pir = []
for i in range(lines):
sticks = ["|" for j in range(i+1)]
self.pir.append(sticks)
def __str__(self):
o = ""
for i, L in enumerate(self.pir):
spaces = (len(self.pir) - i) * " "
o += spaces
o += " ".join(L)
o += "\n"
return o
def stickDelete(self, line, n):
self.pir[line] = self.pir[line][n:]
try:
lines = int(sys.argv[1])
sticks = int(sys.argv[2])
cpir = CPyramide(lines)
print(cpir)
while True:
print("Your turn:")
inputLine = input("Line: ")
inputSticks = input("Matches: ")
if int(inputSticks) > sticks:
print("Error: you cannot remove more than",sticks,"matches per turn")
continue
else:
print("Player removed",inputSticks,"match(es) from line",inputLine)
cpir.stickDelete(int(inputLine) - 1,int(inputSticks))
print(cpir)
print("AI's turn...")
aiSticks = randint(1,sticks)
aiLines = randint(1,lines)
print("AI removed",aiSticks,"match(es) from line",aiLines)
cpir.stickDelete(aiLines - 1,aiSticks)
print(cpir)
I've been trying to make it so it checks every array that contains a [|] and possibly remove it but I don't know how I can make it.
Is there a way I can make the function cpir.stickDelete checks if an array has a [|] in it and possibly remove it but randomly? Because everytime I'm playing with the AI it just chooses something that has already been previously removed.
Is there a way to check through every array and possibly check if it contains a [|] and remove it but randomly?
Thanks for reading.
Try this :
if "|" in my_list:
my_list.remove("|")

How can I write an array to a .txt file, and then fill an array with the same.txt file?

So I'm doing a ToDo app, and I need to save an array of sentences and words to a .txt file. I have done some research but haven't found any tutorials that explain it well enough so I could understand it. As I said I'm using Python 3. Code below:
# Command line TO-DO list
userInput = None
userInput2 = None
userInput3 = None
todo = []
programIsRunning = True
print("Welcome to the TODO list made by Alex Chadwick. Have in mind
that closing the program will result in your TODO"
" list to DISAPPEAR. We are working on correcting that.")
print("Available commands: add (will add item to your list); remove
(will remove item from your list); viewTODO (will"
" show you your TODO list); close (will close the app")
with open('TODOList.txt', 'r+') as f:
while programIsRunning == True:
print("Type in your command: ")
userInput = input("")
if userInput == "add":
print("Enter your item")
userInput2 = input("")
todo.append(userInput2)
continue
elif userInput == "viewTODO":
print(todo)
continue
elif userInput == "remove":
print(todo)
userInput3 = input("")
userInput3 = int(userInput3)
userInput3 -= 1
del todo[userInput3]
continue
elif userInput == "close":
print("Closing TODO list")
programIsRunning = False
continue
else:
print("That is not a valid command")
This sounds like a job for pickle!
Pickle is a built-in module for saving objects and data in Python. To use it, you will need to put import pickle at the top of your program.
To save to a file:
file_Name = "testfile.save" #can be whatever you want
# open the file for writing
fileObject = open(file_Name,'wb')
# this writes the object to the file
pickle.dump(THING_TO_SAVE,fileObject)
# here we close the fileObject
fileObject.close()
To load from a file:
# we open the file for reading
fileObject = open(file_Name,'r')
# load the object from the file
LOADED_THING= pickle.load(fileObject)
fileObject.close()
The code in this answer was taken from here.
I hope that helps!
You use a simple text file to store your TODO - items and retrieve them from it:
import sys
fn = "todo.txt"
def fileExists():
"""https://stackoverflow.com/questions/82831/how-do-i-check-whether-a-fi
answer: https://stackoverflow.com/a/82852/7505395
"""
import os
return os.path.isfile(fn)
def saveFile(todos):
"""Saves your file to disk. One line per todo"""
with open(fn,"w") as f: # will overwrite existent file
for t in todos:
f.write(t)
f.write("\n")
def loadFile():
"""Loads file from disk. yields each line."""
if not fileExists():
raise StopIteration
with open(fn,"r") as f:
for t in f.readlines():
yield t.strip()
def printTodos(todos):
"""Prints todos with numbers before them (1-based)"""
for i,t in enumerate(todos):
print(i + 1, t)
def addTodo(todos):
"""Adds a todo to your list"""
todos.append(input("New todo:"))
return todos
def deleteTodos(todos):
"""Prints the todos, allows removal by todo-number (as printed)."""
printTodos(todos)
i = input("Which number to delete?")
if i.isdigit() and 0 < int(i) <= len(todos): # 1 based
r = todos.pop(int(i) - 1)
print("Deleted: ", r)
else:
print("Invalid input")
return todos
def quit():
i = input("Quitting without saving [Yes] ?").lower()
if i == "yes":
exit(0) # this exits the while True: from menu()
def menu():
"""Main loop for program. Prints menu and calls subroutines based on user input."""
# sets up all available commands and the functions they call, used
# for printing commands and deciding what to do
commands = {"quit": quit, "save" : saveFile, "load" : loadFile,
"print" : printTodos,
"add": addTodo, "delete" : deleteTodos}
# holds the loaded/added todos
todos = []
inp = ""
while True:
print("Commands:", ', '.join(commands))
inp = input().lower().strip()
if inp not in commands:
print("Invalid command.")
continue
# some commands need params or return smth, they are handled below
if inp == "load":
try:
todos = [x for x in commands[inp]() if x] # create list, no ""
except StopIteration:
# file does not exist...
todos = []
elif inp in [ "save","print"]:
if todos:
commands[inp](todos) # call function and pass todos to it
else:
print("No todos to",inp) # print noting to do message
elif inp in ["add", "delete"]:
todos = commands[inp](todos) # functions with return values get
# todos passed and result overwrites
# it
else: # quit and print are handled here
commands[inp]()
def main():
menu()
if __name__ == "__main__":
sys.exit(int(main() or 0))

Appending to a file, then reading from it into a list, then re-appending to it and overwriting certain parts

I want to be able to have a program whereby the user can input a paragraph/sentence/word/character whatever and have that stored in a list e.g. in list[0]. Then I want them to be able to write another bit of text and have that stored in e.g. list[1]. Then at any time I want the user to be able to read that from the list by choosing which segment they want to read from e.g. reading "hello" from list[0] whilst in list[1] "hi" is stored. Then when the user exits the program I want the list to be written to an external file. Then, at next start up, the program should read the file contents and store it again in the list so that the user can add more bits of text or read the current bits. When the list is saved to a file it should append new or changed parts but overwrite parts that are the same so as not to have duplicates. I have attempted this without much success. I am to be honest not sure if it is possible. I have browsed similar forums and have found that hasn't helped much so here it is.
My code so far:
import os
import time
import csv
global write_list
global f1_contents
write_list = []
def write():
os.system("cls")
user_story = input("Enter your text: \n")
write_list.append(user_story)
def read():
os.system("cls")
user_select_needs = True
while user_select_needs == True:
user_select = input("Enter the list section to read from or type exit: \n")
if user_select == "exit":
user_select_needs = False
try:
int(user_select)
select = user_select
select = int(select)
try:
print(write_list[select])
user_select_needs = False
enter = input("Press enter:")
except:
print("There is not stored data on that section!")
except ValueError:
print("That is not a valid section!")
def exit():
os.system("cls")
max_num_needs = True
while max_num_needs == True:
set_max_num = input("Set the storage: \n")
try:
int(set_max_num)
max_num = set_max_num
max_num = int(max_num)
max_num_needs = False
except:
print("It must be an integer!")
for i in range(0, max_num):
f = open("Saves.txt", "a")
f.write(write_list[i])
f.close()
os._exit(1)
def main():
store_num_needs = True
while store_num_needs == True:
set_store_num = input("State the current storage amount: \n")
try:
int(set_store_num)
store_num = set_store_num
store_num = int(store_num)
store_num_needs = False
except:
print("It must be an integer!")
try:
f1 = open("Saves.txt", "r")
for i in range(0, store_num+1):
i, = f1.split("#")
f1.close()
except:
print("--------Loading-------")
time.sleep(1)
while True:
os.system("cls")
user_choice = ""
print("Main Menu" + "\n" + "---------")
print("1) Write")
print("2) Read")
print("3) Exit")
while user_choice not in ["1", "2", "3"]:
user_choice = input("Pick 1, 2 or 3 \n")
if user_choice == "1":
write()
elif user_choice == "2":
read()
else:
exit()
if __name__ == "__main__":
main()
It might be too complicated to understand in which case just ask me in comments- otherwise general tips would be nice aswell.
Thanks in advance
A quick point of correction:
global is only required if you're defining a global variable inside a non-global context. In other words, anything defined at the default indentation level, will be accessible by everything else defined below it. For example:
def set_global():
x = 1
def use_global():
x += 1
set_global()
try:
use_global()
except Exception as e:
# `use_global` doesn't know
# about what `set_global` did
print("ERROR: " + str(e))
# to resolve this we can set `x` to a
# default value in a global context:
x = 1
# or, if it were required, we
# could create a global variable
def make_global():
global x
make_global()
# either will work fine
set_global()
use_global()
print(x) # prints 2
Now to the actual question:
I haven't read through the block of code you wrote (probably best to trim it down to just the relevant bits in the future), but this should solve the problem as I understand it, and you described it.
import os
import sys
user_text = []
# login the user somehow
user_file = 'saves.txt'
def writelines(f, lines):
"""Write lines to file with new line characters"""
f.writelines('\n'.join(lines))
def readlines(f):
"""Get lines from file split on new line characters"""
text = f.read()
return text.split('\n') if text else []
class _Choice(object):
"""Class that is equivalent to a set of choices
Example:
>>> class YesObj(Choice):
>>> options = ('y', 'yes')
>>> Yes = YesObj()
>>> assert Yes == 'yes'
>>> assert Yes == 'y'
>>> # assertions evaluate to True
Override the `options` attribute to make use
"""
allowed = ()
def __eq__(self, other):
try:
s = str(other)
except:
raise TypeError("Cannot compare with non-string")
else:
return s.lower() in self.allowed
def _choice_repr(choices):
allowed = []
for c in choices:
if isinstance(c, _Choice):
allowed.extend(c.allowed)
else:
allowed.append(c)
if len(allowed) > 2:
s = ', '.join([repr(c) for c in allowed[:-1]])
s += ', or %s' % repr(allowed[-1])
elif len(allowed) == 1:
s = '%s or %s' % allowed
else:
s = '%s' % allowed[0]
return s
def _choice_sentinel(name, allowed):
"""Creates a sentinel for comparing options"""
return type(name, (_Choice,), {'allowed': list(allowed)})()
Quit = _choice_sentinel('Quit', ('q', 'quit'))
Yes = _choice_sentinel('Yes', ('y', 'yes'))
No = _choice_sentinel('No', ('n', 'no'))
def readline_generator(f):
"""Generate a file's lines one at a time"""
t = f.readline()
# while the line isn't empty
while bool(t):
yield t
t = f.readline()
def read_from_cache():
"""Overwrite `user_text` with file content"""
if not os.path.isfile(user_file):
open(user_file, 'w').close()
globals()['user_text'] = []
else:
with open(user_file, 'r') as f:
lines = readlines(f)
# replace vs extend user text
for i, t in enumerate(lines):
if i == len(user_text):
user_text.extend(lines[i:])
else:
user_text[i] = t
def write_to_cache():
"""Overwrite cache after the first line disagrees with current text
If modifications have been made near the end of the file, this will
be more efficient than a blindly overwriting the cache."""
with open(user_file, 'r+') as f:
i = -1
last_pos = f.tell()
# enumerate is a generator, not complete list
for i, t in enumerate(readline_generator(f)):
if user_text[i] != t:
# rewind to the line before
# this diff was encountered
f.seek(last_pos)
# set the index back one in
# order to catch the change
i -= 1
break
last_pos = f.tell()
# then cut off remainder of file
f.truncate()
# recall that i is the index of the diff
# replace the rest of it with new
# (and potentially old) content
writelines(f, user_text[i+1:])
def blind_write_to_cache():
"""Blindly overwrite the cache with current text"""
with open(user_file, 'w') as f:
writelines(f, user_text)
def overwrite_user_text(i, text, save=False):
"""Overwrite a line of text
If `save` is True, then these changes are cached
"""
try:
user_text[i] = text
except IndexError:
raise IndexError("No text exists on line %r" % (i+1))
if save:
write_to_cache()
def user_input():
"""Get a new line from the user"""
return raw_input("input text: ")
def user_choice(msg, choices):
if len(choices) == 0:
raise ValueError("No choices were given")
ans = raw_input(msg)
if ans not in choices:
print("Invalid Response: '%s'" % ans)
m = "Respond with %s: " % _choice_repr(choices)
return user_choice(m, choices)
else:
return ans
def user_appends():
"""User adds a new line"""
user_text.append(user_input())
def user_reads(*args, **kwargs):
"""Print a set of lines for the user
Selects text via `user_text[slice(*args)]`
Use 'print_init' in kwargs to choose how
many lines are printed out before user must
scroll by pressing enter, or quit with 'q'."""
print_init = kwargs.get('print_init', 4)
sliced = user_text[slice(*args)]
if not isinstance(sliced, list):
sliced = [sliced]
for i, l in enumerate(sliced):
if i < print_init:
print(l)
sys.stdout.flush()
elif user_choice(l, ['', Quit]) == Quit:
break
def user_changes(i=None, save=False):
"""User changes a preexisting line"""
attempt = True
while i is None and attempt:
# get the line the user wants to change
i_text = raw_input("Line to be changed: ")
try:
# make user input an index
i = int(i_text)
except:
# check if they want to try again
c = user_choice("Bad input - '%s' is not an "
"integer. Try again? " % i_text, (Yes, No))
attempt = (c == Yes)
if attempt:
# user gave a valid integer for indexing
try:
user_reads(i-1)
overwrite_user_text(i-1, user_input(), save)
except Exception as e:
print("ERROR: %s" % e)
if user_choice("Try again? ", (Yes, No)):
user_changes(i, save)
# stores whatever text is already on
# file to `user_text` before use
read_from_cache()

How can I modify this code so it doesn't go back to the beginning of the function, but a little bit after the beginning?

I'm working on a school project and I have a problem. I have to write code for apothecary where clients can buy medicine. So, I need to make restrictions, which one doesn't go with others and etc. Here is the code:
def prodajLek():
lekovi = Fajl1.UcitavanjeLekova()
lekoviRed = []
brojacZaForPetlju = 1
n = 0
cena = 0
kolicina = []
korpa = []
rednibrojevilekova = []
ukupnacena = 0
print(" Fabricki naziv Genericki naziv Serijski broj Kolicina Cena \n")
for i in lekovi:
x = i.strip().split("|")
lekoviRed.append(x)
if lekoviRed[n][5] == "False":
print(brojacZaForPetlju,"\t {:10} \t {:10} \t\t\t {:3} \t\t\t {:4} \t\t {:5}".format(x[0],x[1],x[2],x[3],x[4]))
brojacZaForPetlju = brojacZaForPetlju + 1
n = n + 1
print("\n\n\n\n")
rednibrleka = input("Izaberite redni broj leka koji zelite da prodate:\n>>\t")
rednibrleka = int(rednibrleka)
rednibrleka = rednibrleka - 1
rednibrojevilekova.append(rednibrleka)
kolicinaZahteva = input("Koju kolicinu zelite da prodate?\n>>\t")
kolicinaZahteva = int(kolicinaZahteva)
if kolicinaZahteva > int(lekoviRed[rednibrleka][3]):
print("Nema toliko na lageru!\n")
Fajl1.LekarMenu()
kolicina.append(kolicinaZahteva)
cena = int(lekoviRed[rednibrleka][4])
korpa.append(cena)
print("Da li zelite da kupite jos lekova?\n1.Da\n2.Ne\n")
nastavakKupovine = input(">>")
if nastavakKupovine == "1":
prodajLek()
elif nastavakKupovine == "2":
Fajl1.LekarMenu()
So, when I get to the nastavakKupovine input, when I press 1, I need to continue shopping and store my row numbers, my price and quantity in arrays rednibrojlekova = [] , korpa = [] and kolicina = []. But I have a problem, because I dont know how to continue this without reseting these arrays to empty.
The standard idiom for what you want to do is a while True loop. Rather than show how to change your (rather long) function, here's a very simple one which hopefully shows the principle in a straightforward way:
def ask():
answers = []
while True:
response = input("What do you have to say? ")
answers.append(response)
check = input("Type 'q' to quit, anything else to repeat: ")
if check == "q":
break
else:
continue
return answers
For this simple function, the else: continue part isn't necessary, because the loop will continue anyway, but I've included it so you can see how to use it.
Here's an example of the function in action:
>>> ask()
What do you have to say? Something
Type 'q' to quit, anything else to repeat:
What do you have to say? Another thing
Type 'q' to quit, anything else to repeat:
What do you have to say? Ok, done
Type 'q' to quit, anything else to repeat: q
['Something', 'Another thing', 'Ok, done']
>>>
You can find out more about while, break and continue by reading the More Control Flow Tools chapter of the official Python tutorial.

(python) append used to list, then only return value if it hasn't been used

The title pretty much says it all. Following is the barebones of a small Japanese learning game that I wrote. I need to only print the kana if it has not already been printed in the current loop. Can anyone see what it is that I'm doing wrong? Thank you :)
#!/usr/bin/python
from os import system as cmd
from random import choice as random
from time import sleep
from sys import platform
m = ["ma", "mi", "mu", "me", "mo"]
y = ["ya", "yu", "yo"]
n = ["n"]
def get_dict(group):
if group == 1:
return m
elif group == 13:
return y
elif group == 14:
return n
elif group == 15:
return m + y + n
def clear():
if "win" in platform: cmd("cls")
if "lin" in platform: cmd("clear")
def get_kana():
global kana
return random(kana)
## Initiate ##
kana = get_dict(15)
speed = speed()
clear()
print disp
print "Please get ready!..."
sleep(5)
def chk_used():
global used_kana
numlpo = 0
while numlpo < 50:
numlpo = numlpo + 1
kana = get_kana()
if kana not in used_kana:
used_kana = used_kana.append(kana)
return kana
break
def main():
used_kana = []
while True:
clear()
print disp
print "Please write the following kana: \n"
print " " + chk_used()
sleep(3)
main()
A couple things:
In chk_used, you have the line:
used_kana = used_kana.append(kana)
There's only one problem. list.append() returns None. Every time you do this, you're appending kana to used_kana, but then you're setting the value of used_kana to None.
used_kana.append(kana)
will suffice
You define used_kana within a function
def main():
used_kana = []
But you try to refer to it globally. Python looks for a global variable and won't find one.
def chk_used():
global used_kana
The solution:
pass used kana as an argument to chk_used()
def chk_used(usedCharacters):
...
def main():
used_kana = []
...
print " " + chk_used(used_kana)
Finally:
if kana not in used_kana:
used_kana.append(kana)
return kana
break #there's no reason for this break. the return on the line before exits the function, your code will never ever execute this line.

Categories