質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

17366閲覧

TypeError: can't multiply sequence by non-int of type 'str'の原因を知りたい

khayato0512

総合スコア24

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2019/04/20 02:09

前提・実現したいこと

今、codacademyで、様々な配送のコスト計算と、一番安い配送方法をプログラムする課題をやっています。
3つ目のファンクションで、重量に対してどの配送方法が一番安いか調べて、その結果と共に”the cheapest shipping way is ~~ and the price is $~~!.と出したいのですが、そのreturnの文に対してエラーが出てしまいます。どこがいけないのでしょうか?

発生している問題・エラーメッセージ

TypeError: can't multiply sequence by non-int of type 'str'

該当のソースコード

python3

1def ground_shipping(weight): 2 if weight<=2.0: 3 cost=weight*1.50+20.00 4 if weight>2.0 and weight<=6.0: 5 cost=weight*3.00+20.00 6 if weight>6.0 and weight<=10.0: 7 8 cost=weight*4.00+20.00 9 if weight>10.0: 10 cost=weight*4.75+20.00 11 return cost 12 13premium_shipping=125.00 14 15def drone_shipping(weight): 16 if weight<=2.0: 17 cost=weight*4.50 18 if weight>2.0 and weight<=6.0: 19 cost=weight*9.00 20 if weight>6.0 and weight<=10.0: 21 cost=weight*12.00 22 if weight>10: 23 cost=weight*14.25 24 return cost 25 26def the_cheapest_shipping(weight): 27 28 if ground_shipping(weight)<premium_shipping and ground_shipping(weight)<drone_shipping(weight): 29 the_cheapest_way="ground shipping" 30 the_price=ground_shipping(weight) 31 if premium_shipping<ground_shipping(weight) and premium_shipping<drone_shipping(weight): 32 the_cheapest_way="premium shipping" 33 the_price=premium_shipping 34 else : 35 the_cheapest_way="drone shiipping" 36 the_price=drone_shipping(weight) 37 return ("the cheapest shipping way is "+the_cheapest_way+" and the price is $"*str(the_price)+" !.") 38 39 40 41 42print(the_cheapest_shipping(8.6))

試したこと

ここに問題に対して試したことを記載してください。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

TypeError: can't multiply sequence by non-int of type 'str'

pythonは「str型変数は掛け算できないよ」と言って困っています。

掛け算しているのはweightだけのようですので、おそらくweightがstr型のままなのではないでしょうか?

int(weight)float(weight)で数値型に変換してあげれば掛け算できるようになります。

投稿2019/04/20 02:24

siruku6

総合スコア1382

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

siruku6

2019/04/20 02:27 編集

あ、違う。 これタイピングミスじゃないですか。 return ("the cheapest shipping way is "+the_cheapest_way+" and the price is $"*str(the_price)+" !.") の部分の * を + にしてあげて下さい。
khayato0512

2019/04/20 02:27

回答ありがとうございます。質問なのですが、 全てのweightをint(weight)にすると言うことですか?
khayato0512

2019/04/20 02:28

あ!!とてもつまらないミスをしていました。すみません。ありがとうございます!!助かりました!
siruku6

2019/04/20 02:33

よかったです! 自力で気付くのは難しいタイプのミスでしたね。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問