2017-09-28 8 views
-1

私は、クラス内の男性と女性の数を考慮して、クラス内の男性と女性の割合を計算するプログラムを作成しようとしています。ここでシンプルなPython百科事典

は私が持っているものです。

# Calculate percentage of males and females in a class 
males = input("Enter the number of males in the class: ") 
females = input("Enter the number of females in the class: ") 
total = int(males + females) 
mperc = males*100/total 
fperc = females*100/total 
print ("The class is" mperc, "percent male and" fperc, "percent female") 

検証は私に(印刷ラインで)「mperc」に関する間違った構文を語っています。 私はPytonには新しく、これを修正する方法がわからない。どんな助けも素晴らしいだろう!

答えて

0

mperc = males*100/totalは、文字列をintで分割しているため失敗します。

males = int(input("Enter the number of males in the class: ")) 
females ... # dito 

そうでない場合は、total = int(males + females)は、文字列を連結し、唯一intに連結の結果を変換する:あなたは最高のすぐintに(文字列で)あなたの入力を変換

あなたはPython2を使用している場合は、除算を整数にするために、代わりにフロートを使用することがあります:

males = float(input("Enter the number of males in the class: ")) 
1

あなたが並んで配置するのでライン

print ("The class is" mperc, "percent male and" fperc, "percent female") 

で構文の問題があります文字列と変数。カンマで区切るか、文字列として結合する必要があります。

print("The class is", mperc, "percent male and", fperc, "percent female") 

とする必要があります。

それでもなお、schwobasegglによるコメントは適用されます。

関連する問題