2017-12-06 11 views
0

私はあなたの提案どおりに自分のコードを修正しました。私はループに入ることができますが、正しい名前を入力すると、まだそれから出ることはできません。助言がありますか? は、ここで私が得たものです:FYI :私は入力を与えるときwhileループが正しいデータを出力していない

import csv 

full_name = input('Enter your full name: ').lower() 

with open('Report1.csv') as csvfile: 
    hour_summation = {} 
    read_csv = csv.reader(csvfile, delimiter=',') 
    for row in read_csv: 
     while (' '.join((row[0], row[1]))).lower() != full_name.strip().lower(): 
      print('Name is not in system') 
      full_name = input('Enter your full name: ').lower() 
     if(' '.join((row[0], row[1]))).lower() == full_name.strip().lower(): 
      hour_summation[row[2]] = hour_summation.get(row[2], 0) + int(float(row[3])) 
print('This is {} full hours report:'.format(full_name)) 
for k, v in hour_summation.items(): 
    print(k + ': ' + str(v) + ' hours') 

はここでの結果です。 Steve Millerはcsvファイルにないので、最初の応答は正しいです。しかし、Sri Mantriがファイルに入っていますので、彼女の名前の下にすべてのリストを印刷してください。

Enter your full name: Steve Miller 
Name is not in system 
Enter your full name: Sri Mantri 
Name is not in system 

コード実行時の出力は次のようになります。

Enter your full name: Sri mantri 
This is sri mantri full hours report: 
Beeline Blank: 28 hours 
SRV-0001 Service Requests for Base and Direct Services: 4 hours 
SUP-0001 Support Requests with a CISM Ticket: 129 hours 
SUP-2503 Web Application Maintenance & Support: 72 hours 
0026184229 Margin Controlling Java Rewrite: 4 hours 
0033472751 PRE-AFE 2017 - CMS Enhancements: 2 hours 
0033472863 PRE-AFE 2017 - BPM Enhancements: 67 hours 
APP-10008 Pre-Series (Non-Mainframe): 4 hours 
APP-10146 Logistics (Non-Mainframe): 3 hours 
APP-10195 Vehicle Labor System (Mainframe): 3 hours 
APP-10354 Web PartsPro (Non-Mainframe): 1 hours 
APP-10431 VIPService (Non-Mainframe): 1 hours 
APP-10432 VIPService (Mainframe): 3 hours 
APP-10536 Truck Invoice Adjustments (Mainframe): 2 hours 

とCSVファイルを次のようになります。あなたが名前を取得するためにnameを使用しますが、後であなたが唯一のfull_nameを使う(そしてそれすべきfull_name

while (' '.join((row[0], row[1]))).lower() != full_name.strip(): 
     print('Name is not in system') 
     name = input('Enter your full name: ') 

を使用して、このコードの中

First Name Last Name Activity Hours 
Sri Mantri SUP-2503 Web Application Maintenance & Support 11 
Sri Mantri SUP-2503 Web Application Maintenance & Support 3 
Sri Mantri SUP-2503 Web Application Maintenance & Support 5 
Sri Mantri SUP-2503 Web Application Maintenance & Support 2 
Jeff Moore SUP-2503 Web Application Maintenance & Support 3 
David Ayers SUP-2507 NAFTA MFTS OS Support 10 
Prasanth Musunuru 0020826809 Vertex 6.0 at the NDC 4 
Prasanth Musunuru 0020826809 Vertex 6.0 at the NDC 3 
Prasanth Musunuru 0020826809 Vertex 6.0 at the NDC 1 
Prasanth Musunuru 0020826809 Vertex 6.0 at the NDC 1 
Jeff Moore 0024480049 Fuel Tanks (infrastructure) - time tracking 1 
Jeff Moore 0024480049 Fuel Tanks (infrastructure) - time tracking 1 
Jeff Moore 0024480049 Fuel Tanks (infrastructure) - time tracking 4 
+0

ようこそStackOverflowへ。ヘルプドキュメントの投稿ガイドラインを読み、それに従ってください。 [最小、完全で検証可能な例](http://stackoverflow.com/help/mcve)がここに適用されます。 MCVEコードを投稿して問題を正確に記述するまでは、効果的にお手伝いすることはできません。 投稿したコードをテキストファイルに貼り付け、説明した問題を再現できるはずです。 この場合、データファイルを指定し、検索対象の名前をハードコードしてください。 – Prune

+0

'print()'を使って変数に値を表示すると、それらが異なっているかどうかが分かります。 – furas

+0

なぜ 'name'を使って2番目の' input() 'を取得し、' full_name'をチェックしますか? 'full_name'だけを使うべきです。 – furas

答えて

1

ニーズlower()

while (' '.join((row[0], row[1]))).lower() != full_name.strip(): 
     print('Name is not in system') 
     full_name = input('Enter your full name: ').lower() # <-- full_name 

か、FULL_NAMEする名前を結ぶあなたのwhileループ内のライン欠けているname

full_nameから
while (' '.join((row[0], row[1]))).lower() != full_name.strip(): 
     print('Name is not in system') 
     name = input('Enter your full name: ') 
     full_name = name.lower() # <-- full_name 
+0

下記の変更を見てください。 – stevenmiller

1

変換:

full_name = name.lower() 

だけの入力より、あなたのwhileループでこれを追加しますあなたのファイルの先頭にあるように、()コールを呼び出します。あなたのwhileループ内

は、それは言う必要があります:

name = input('Enter your full name: ') 
full_name = name.lower() 

実行するようにプログラムを取得すること、さらに、少なくとも!

また、プログラムのロジックに欠陥がある可能性があることに注意してください... CSVの各行をステップ実行して、人の名前を確認します。これは、CSVファイルに複数のエントリがある場合(つまり、CSVファイルに複数の人の情報が格納されている場合)、リストの最初のエントリに実際にアクセスできます。 CSVの各行を実行して一致するかどうかを確認するために、おそらくユーザ名を入力する必要があります。 CSV全体に一致するものがない場合にのみ、別の名前を尋ねる必要があります。ちょっと見てください...

1

実際に他の解決策に興味がある場合は、私の回答に進みます。パンダでは、あなたの必要性を解決する方が簡単です。

私はCSVファイル( "名前。

import pandas 

if __name__ == "__main__": 
    csv = pandas.read_csv("names.csv") 
    name = input("Enter your name: ") 
    if (csv["Name"]==name).any(): 
     print("Name Present") 
     correct_name = csv.loc[csv["Name"] == name] 
     # print everything 
     print(csv) 
     print() 
     #print correct name 
     print(correct_name) 
     print() # for clear display 
     # get individual things 
     print("Correct Inidividual Values") 
     print(correct_name.values) 
    else: 
     print("Name not there") 

サンプル入力と出力::

Enter your name: Steve Miller 
Name not there 

次の実行、

Enter your name: Sri Mati 
Name Present 
     Name Hours 
0 Sri Mati  1 
1 Some Name  2 

     Name Hours 
0 Sri Mati  1 

Correct Inidividual Values 
[['Sri Mati' 1]] 
その中にこの値を使用してCSV」)、

Name Hours 
Sri Mati 1 
Some Name 2 

ここでは、コードです

関連する問題