Keys and Dictionaries - python

it seems that my code isn't working well, everytime i run it it seems that the key value of agenda is replaced by the next one I enter instead of adding it as a new one. Please help my find why is this happening and how to i fix it. Thanks!
def contactos():
q=int(raw_input("Desea agregar un contacto (1=si, 0=no): "))
while q==1:
a=raw_input("ingrese nombre contacto, telefono, mail (delimitados por espacio)")
d=a.split()
agenda={}
agenda[d[0]]= "nombre", d[0], "telefono: ", d[1], "mail :", d[2]
q=int(raw_input("Desea agregar otro contacto (1=si, 0=no): "))
print agenda.keys()
return agenda

You are resetting agenda to an empty dictionary each time through the loop. Initialize it once before the loop.
def contactos():
agenda = {}
q=int(raw_input("Desea agregar un contacto (1=si, 0=no): "))
while q==1:
a=raw_input("ingrese nombre contacto, telefono, mail (delimitados por espacio)")
d=a.split()
agenda[d[0]]= "nombre", d[0], "telefono: ", d[1], "mail :", d[2]
q=int(raw_input("Desea agregar otro contacto (1=si, 0=no): "))
print agenda.keys()
Do note Cyber's answer, as well, if you want to add multiple phone numbers and addresses for the same name.

Instead of assigning, which will overwrite the value
agenda[d[0]]= "nombre", d[0], "telefono: ", d[1], "mail :", d[2]
You would have to append
agenda[d[0]].append(["nombre", d[0], "telefono: ", d[1], "mail :", d[2]])

you should/could use class instead
https://docs.python.org/2/reference/datamodel.html
here is a very simple example
class contact:
def __init__(self, name="", phone="", mail=""):
self.phone = phone
self.name = name
self.mail = mail
a = contact("A name")
print a.name
a.mail = "test#gmail.com"
print a.mail

Related

How to override lists?

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))

A problem from python crash course by google

The group_list function accepts a group name and a list of members, and returns a string with the format: group_name: member1, member2, … For example, group_list("g", ["a","b","c"]) returns "g: a, b, c". Fill in the gaps in this function to do that.
def group_list(group, users):
members = ___
return ___
print(group_list("Marketing", ["Mike", "Karen", "Jake", "Tasha"])) # Should be "Marketing: Mike, Karen, Jake, Tasha"
print(group_list("Engineering", ["Kim", "Jay", "Tom"])) # Should be "Engineering: Kim, Jay, Tom"
print(group_list("Users", "")) # Should be "Users:"
I have tried sth like this:
members = " "
for i in users:
members += ",".join(i)
return ("{}:{}".format(group, members))
output comes:
Marketing: M,i,k,eK,a,r,e,nJ,a,k,eT,a,s,h,a
Engineering: K,i,mJ,a,yT,o,m
Users:
but it didn't give the expected answer. Can anyone solve it with filling the blanks please ?
You need to join users, not each user separately.
members = ', '.join(users)
return "{}: {}".format(group, members)
def group_list(group, users):
members =", ".join(users)
return(" {}: {}".format(group, members))
print(group_list("Marketing", ["Mike", "Karen", "Jake", "Tasha"])) # Should be "Marketing: Mike, Karen, Jake, Tasha"
print(group_list("Engineering", ["Kim", "Jay", "Tom"])) # Should be "Engineering: Kim, Jay, Tom"
print(group_list("Users", "")) # Should be "Users:"
Try this:
return group + ": " + (", ".join(users))
Check out a more shorter way to achieve same result using the format 'f' method.
def group_list(group, users):
members = ", ".join(users)
return f"{group}: {members}"
Try this one
def group_list(group, users):
members = " "
for i in users:`
members+=",".join(users)
break
return "{}:{}".format(group,members)**
print(group_list("Marketing", ["Mike", "Karen", "Jake", "Tasha"])) # Should be "Marketing: Mike, Karen, Jake, Tasha"
print(group_list("Engineering", ["Kim", "Jay", "Tom"])) # Should be "Engineering: Kim, Jay, Tom"
print(group_list("Users", "")) # Should be "Users:"
Check this out:
def group_list(group, users):
members = ", ".join(users)
return "{}: {}".format(group,members)
This should give desired results:
def group_list(group, users):
members = ""
for user in users:
members += user + ", "
return "{}: ".format(group) + members

Creating a subject and grading system in Python

I am trying to create a gradingsystem for a UNI project.
we are told to have 3 global lists:
Emner = ["INFO100","INFO104","INFO110","INFO150","INFO125"]
FagKoder = [["Informasjonsvitenskap","INF"],["Kognitiv vitenskap","KVT"]
Karakterer=[["INFO100","C"],["INFO104","B"],["INFO110","E"]]
With these lists we are suppost to create a way to view the subjects(Emner), with grades from Karakterer, but we should also be able to view subjects without grades. It should be displayed like this:
We should also be able to add new subjects in (Emner) and add new Grades in (Karakterer). All of this should be displayed as in the picture above.
I have been trying all different kind of ways of doing this, but i keep returning to one of two problems. Either im not able to print a subject without a grade, or if i add a new subject(Emne), and want to add a grade(Karakter) i am not able to place it to the right Subject, as it just saves at the first one without a grade.
hope anyone can help me with this, going crazy here!
Code i have so far:
def emneliste():
global Emner
global Karakterer
emne,kar = zip(*Karakterer)
ans = [list(filter(None, i)) for i in itertools.zip_longest(Emner,kar)]
def LeggTilEmne():
global Karakterer
global Emner
nyttEmne = input("Skriv ny emnekode (4Bokstaver + 3 tall): ")
if nyttEmne not in Emner:
while re.match('^[A-Å]{3,4}[0-9]{3}$',nyttEmne):
Emner.append(nyttEmne)
print(nyttEmne + " Er lagt til!")
start()
print("Feil format")
LeggTilEmne()
else:
print("Dette Emnet er allerede i listen din")
start()
def SettKarakter():
global Karakterer
global Emner
VelgEmne = input("Hvilke emne? ")
Emne,Karakter = zip(*Karakterer)
if str(VelgEmne) not in str(Emner):
print("Dette faget er ikke i din liste")
feil = input("om du heller ønsket å opprette fag trykk 2, ellers trykk enter ")
if feil == str(2):
LeggTilEmne()
else:
start()
else:
if str(VelgEmne) in str(Karakterer):
index = Karakterer.index([VelgEmne,"C"])
Karakterer.pop(index)
SettKar = input("Karakter? ")
Emner.append([VelgEmne,SettKar])
print("Karakter " + SettKar + " Er Lagt til i " + VelgEmne)
start()
else:
SettKar = input("Karakter? ")
if str(VelgEmne) in str(Emner):
index = Emner.index(VelgEmne)
print(index)
Emner.pop(index)
Emner.insert(index,[VelgEmne,SettKar])
print("Karakter " + SettKar + " Er Lagt til i " + VelgEmne)
start()
else:
print("Virker Ikke")
start()
You can make Karakterer a dict instead so that you can iterate through the subjects in Emner and efficiently look up if a subject is in Karakterer with the in operator:
Karakterer = dict(Karakterer)
for subject in Emner:
print(*([subject] + ([Karakterer[subject]] if subject in Karakterer else [])))
This outputs:
INFO100 C
INFO104 B
INFO110 E
INFO150
INFO125
Here's an updated GradeHandler class demo. I tried to allow for updating grades, removing subjects, etc.:
__name__ = 'DEMO'
class GradeHandler(object):
EMNER = ["INFO100","INFO104","INFO110","INFO150","INFO125"]
FAGKODER= [["Informasjonsvitenskap","INF"],["Kognitiv vitenskap","KVT"]]
KARAKTERER = [["INFO100","C"],["INFO104","B"],["INFO110","E"]]
def __init__(self):
self.Emner = self.EMNER
self.FagKoder = self.FAGKODER
self.Karakterer = self.KARAKTERER
self.__create_grade_dict()
def remove_subject(self, subject_name):
"""
Remove a subject ot the classes class list variable.
"""
try:
self.Emner = [i for i in self.EMNER if i != subject_name]
self.__create_grade_dict()
except ValueError:
pass
def add_subject(self, subject_name):
"""
Append a subject ot the classes class list variable.
"""
if not subject_name in Emner:
self.Emner.append(subject_name)
self.__create_grade_dict()
def __create_grade_dict(self, grade_dict=None):
"""
Split grades matrix into separate parts; Create and set a dictionary of values.
"""
if grade_dict is None:
self.grade_dict = dict()
sub, grade = zip(*self.Karakterer)
karakterer_dict = {k:v for k, v in list(zip(sub, grade))}
for i in self.Emner:
if i in karakterer_dict.keys():
self.grade_dict[i] = karakterer_dict[i]
else:
self.grade_dict[i] = ''
def update_grade(self, subject_name, grade='A'):
"""
Update a grade in the grade dictionary.
Will also add a subject if not alrady in the dictionary.
"""
try:
self.grade_dict[subject_name] = grade
except (KeyError, ValueError):
pass
def print_grades(self, subject_name=None):
"""
Print dictionary results.
"""
if subject_name is None:
for k, v in self.grade_dict.items():
print('{} {}'.format(k, v))
else:
if subject_name in self.grade_dict.keys():
print('{} {}'.format(subject_name, self.grade_dict[subject_name]))
if __name__ == 'DEMO':
### Create an instance of the GradeHandler and print initial grades.
gh = GradeHandler()
gh.print_grades()
### Append a class
gh.add_subject('GE0124')
gh.print_grades()
### Add grade
gh.update_grade('GE0124', 'B+')
gh.print_grades()
### Update grades
gh.update_grade('GE0124', 'A-')
gh.print_grades()
### Remove subject (will also remove grade.
gh.remove_subject('GE0124')
gh.print_grades()

how do i fix this function with dictionary to add and print added people?

currently im trying to solve a problem in the code i wrote, for some reason whenever i call imprimir() it only shows the last added person while it should show all the persons added.
libro = {}
def agregar(rut, nombre, edad):
estudiante = {}
estudiante['rut'] = rut
estudiante['nombre'] = nombre
estudiante['edad'] = edad
libro['rut'] = estudiante
def imprimir():
for rut in libro:
estudiante = libro[rut]
print(estudiante['rut'], estudiante['nombre'], estudiante['edad'])
def main():
contador = 0
while contador < 2:
rut = input("rut: ")
nombre = input("nombre: ")
edad = input("Edad: ")
contador = contador + 1
agregar(rut, nombre, edad)
imprimir()
main()
I had the code limited to only 2 people to be added. so if for the first person i write, rut = 1, nombre = 1 and edad = 1, and for the second, rut = 2, nombre = 2 and edad = 2. While using main(), it should print:
1 1 1
2 2 2
but instead it just prints 2 2 2 with 1 1 1 not found, my guess is that somehow the added person are not added but instead rewritten, but i cant find why, if i includo imprimir() inside the while in the main() it prints each person as soon as i finish adding one, but the idea its that the program should print all the added people once i finish adding them.
The problem is your adding the items with the same key so the dict just overrides the last value with each entry you add...
libro = {}
def agregar(rut, nombre, edad):
estudiante = {}
estudiante['rut'] = rut
estudiante['nombre'] = nombre
estudiante['edad'] = edad
# this would override the previous entry since it's always the same key
libro['rut'] = estudiante
# use a key that is unique and all entries will be preserved
libro[rut] = estudiante
# or for some truly unique keys
libro[str(uuid.uuid4())] = estudiante

NameError: global name 'Circulo_Mohr_v1_2' is not defined

I'm trying make a simple menu (options: 1,2,3) and the second option (input 2) should run a graphical menu.
When I try run python reports a NameError ("global name 'Circulo_Mohr_v1_2' is not defined").
I don't know the correct syntax
print "inicio"
import sys
from librerias import Circ_Mohr_motor_v2
import librerias.Circulo_Mohr_v1_2
from librerias import prueba_importacion
'''
def definicion_ventana():
Circulo_Mohr_v3_0.Ui_CalculodecirculosMohr()
#Ui_CalculodecirculosMohr.setupUi()
'''
def seleccion_de_libreria():
print '''Escoger opcion:
1) motor
2) Ventana
3) test
'''
opcion = raw_input ("Opcion seleccionada: ")
opcion = int (opcion)
if opcion == 1:
print "se ejecuta el motor de calculo"
punto_Ax = raw_input ("Insertar coordenada X de primer punto: ")
punto_Ay = raw_input ("Insertar coordenada Y de primer punto: ")
punto_Bx = raw_input ("Insertar coordenada X de segundo punto: ")
punto_By = raw_input ("Insertar coordenada Y de segundo punto: ")
Circ_Mohr_motor_v2.circulo_mohr(punto_Ax,punto_Ay,punto_Bx,punto_By)
elif opcion == 2:
print "se ejecuta la funcion ventana"
Circulo_Mohr_v1_2.Ui_CalculodecirculosMohr()
print "fin la funcion ventana"
else:
print "se ejecuta el test"
prueba_importacion.prueba_01()
seleccion_de_libreria()
print "fin"
How can I fix that?
try replace
import librerias.Circulo_Mohr_v1_2
with
from librerias.Circulo_Mohr_v1_2 import Ui_CalculodecirculosMohwith
and call directly Ui_CalculodecirculosMohr()
Ui_CalculodecirculosMohr()

Categories