2017-09-24 7 views
-3

私の割り当てはここでstrとintの型を取得するのはなぜですか?

従業員の名前と給与を取得するプログラムを作成します。 連邦税および州税は、次の基準に基づいて計算します。 給与が100000を超える場合は、連邦税を20%で計算します。 連邦税を15%と計算します。州税を計算します。 5%従業員の給与。ネット 給与を計算するには、給与総額から連邦および州税を差し引きます。

マイコード:

employeename = input("Enter the employee's name:") 
grosssalary = input("Enter the employee's gross salary: $") 
if grosssalary > 100000: 
    federaltax = 0.20 
else: 
    federaltax = 0.15 
statetax = 0.05 
netsalary = float(grosssalary) - float(grosssalary * federaltax) - float(grosssalary * statetax) 
print (employeename,"'s net salary is $",netsalary) 

出力:

Enter the employee's name:Ali 
Enter the employee's gross salary: $1000 
Traceback (most recent call last): 
    File "/home/ubuntu/workspace/Untitled4", line 3, in <module> 
    if grosssalary > 100000: 
TypeError: unorderable types: str() > int() 
Process exited with code: 1 
+0

まず、文字列をintに変換する必要があります。 –

+0

これは確かに重複しています... –

答えて

1

input()戻りは、Python 3.xでstrを入力

grosssalary > 100000str > intです。 、使用解決するために

:Pythonの3.xでは

gross_salary = int(input("Enter the employee's gross salary: $")) 
1

を、input()の戻り値は常にタイプstrのであるので、あなたは例外TypeErrorを得intオブジェクトにstrオブジェクトを比較しています。あなたはそれを行うことはできません、比較する前にそれをintまたはfloatに変換する必要があります。

これを試してみてください:

grosssalary = float(input("Enter the employee's gross salary: $")) 
0

エラーは、文字列がintよりも大きく、それが受け入れられない場合はチェックしてみてくださいことは明らかです。

あなたが最初のintにあなたの文字列を変換してからチェックを行う必要があり、あなたのコードがどのように見えるはずです。

employeename = input("Enter the employee's name:") 
grosssalary = input("Enter the employee's gross salary: $") 
if int(grosssalary) > 100000: 
    federaltax = 0.20 
else: 
    federaltax = 0.15 
statetax = 0.05 
netsalary = float(grosssalary) - float(grosssalary * federaltax) - float(grosssalary * statetax) 
print (employeename,"'s net salary is $",netsalary) 

文字列がintであれば、あなたもあなたのチェックがすべきことを行うには、最初に確認する必要があります次のようになります。

if grosssalary.isdigit() 

    if int(grosssalary) > 100000: 

     federaltax = 0.20 
    else: 
     federaltax = 0.15 
else: 
    print("bad entry") 
関連する問題