I was supposed to insert new items to a dictionary, and the new items would be decided by the user input. I have tried three differents things (the ones marked as comments), but none is working. Does anyone know how to fix it?
butikk = {"melk": 14.9, "broed": 24.9, "yoghurt": 12.9, "pizza": 39.9}
print(butikk)
ny_vare = str(input("Skriv inn en matvare og prisen: "))
ny_vare_pris = float(input("Hvor mye koster varen? "))
ny_vare1 = str(input("Skriv inn en matvare: "))
ny_vare1_pris = float(input("Hvor mye koster varen? ")
#butikk.append(ny_vare)
#butikk.append(ny_vare1)
#butikk[ny_vare] = ny_vare_pris
#butikk[ny_vare1] = ny_vare1_pris
#butikk.update(ny_vare : ny_vare_pris)
#butikk.update(ny_vare1 : ny_vare1_pris)
print(butikk)
Okay, so your problem is solved. To be noted, you had done 2 things wrong here.
First , you missed a bracket on
line 8
andSecond one is that you should apply
{}
to update the dictionary.
Let me show you the correct code:
butikk = {
"melk": 14.9,
"broed": 24.9,
"yoghurt": 12.9,
"pizza": 39.9
}
ny_vare = input("Skriv inn en matvare og prisen: ")
ny_vare_pris = float(input("Hvor mye koster varen? "))
ny_vare1 = input("Skriv inn en matvare: ")
ny_vare1_pris = float(input("Hvor mye koster varen? "))
butikk.update({ny_vare: ny_vare_pris})
butikk.update({ny_vare1: ny_vare1_pris})
print(butikk)
Now you will get your desired output.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.