0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

価格の算出 Python3編

Posted at

価格リストを作ってその中から買い物リストにあるモノの値段を出力せよという問題。

前のがヒントになってこれは解けた。
一瞬難しかったのが、name,price = map(str,input().split())
数値の部分を数値にせず文字列のままにしたが
問題なく検索できたのでそのあとも最後までできた。

N, M = map(int,input().split())
prices = {}
for i in range(N):
    name,price = map(str,input().split())
    prices[name] = price
    
for order in range(M):
    item = input()
    if item not in prices:
        print(-1)
    else:
        print(prices[item])

で、答えを見てみると
どうやらひっかかったところ、こうすればよかったらしい。
たしかにそうだ。
splitのときだけ文字列のままにしといて、あとで型変換するという方法は
覚えておこう。

prices[name] = int(price)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?