0
Removing ' and , from printed lists and use variables with strings in python dictionary list
This is what I have: import random pet={ 'name':'temp', } #assigns pet a new name pet['name']=input('please name your pet') phrase=['meow',(pet['name'],'hisses at you')] #main loop quit=False while not quit: user=input() if user=='chat': print(random.choice(phrase)) elif user=='quit': quit=True Output let's me change the name and then use the chat option but when the hiss chat comes up it prints "('Kitty', 'hisses at you')" I need to remove the ' and , from the out put and it would be preferable to store the list in the dictionary as well. Any pointers?
4 Antworten
+ 2
# Hi! Try use string formatting when you print. Example:
myList = ["a1", "b2", "c2"]
print(f"{myList[0]} Hello {myList[1]} world {myList[2]}")
# >>> a1 Hello b2 world c2
+ 1
# Hi! You can put what ever you want inside the string formating brackets:
myDict = {"myKey": "myValue"}
print(f"{myDict['myKey']}")
# >>> myValue
+ 1
You taught me something new today and I highly appreciate it, thank you!
0
That works wonders with a list outside of of the dictionary. Any idea how to implement this in the dictionary?