简体   繁体   English

While循环未输出正确的数据

[英]While loop isn't outputting the correct data

So I made corrections to my code as you all suggested. 因此,正如大家所建议的,我对我的代码进行了更正。 I'm able to get into the loop however once I input a correct name I'm still not able to get out of it. 我可以进入循环,但是一旦输入正确的名称,我仍然无法摆脱它。 Any suggestions? 有什么建议么? Here's what I got: 这是我得到的:

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')

Here's the result when I give an input: fyi. 当我输入时,结果如下:fyi。 Steve Miller is not in the csv file so that first response is correct. Steve Miller不在csv文件中,因此第一个响应是正确的。 However, Sri Mantri is in the file and it should continue on and print out all the listings under her name. 但是,Sri Mantri已保存在文件中,应该继续并以她的名字打印所有清单。

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

Here's what the output should look like when the code runs. 这是代码运行时输出的外观。

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

and the csv looks like this: 和CSV看起来像这样:

   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

Inside this code you use name to get name but later you use full_name 在此代码内,您使用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: ')

You should use only full_name (and it needs lower() ) 您应该只使用full_name (并且需要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

Or convert name to full_name 或将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

You're missing a line in your while loop that connects name to full_name: 您在while循环中缺少将name连接到full_name的行:

full_name = name.lower()

Add this in your while loop, just below the input() call, like you have towards the top of your file. 将此添加到您的while循环中,就在input()调用的下面,就像您将文件顶部放置的那样。

Inside your while loop, it should say: while循环中,它应该说:

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

That will get your program to run, further, at least! 至少,这将使您的程序进一步运行!

Also, please note that the logic of your program is probably flawed... You step through each line of the CSV, checking for a person's name. 另外,请注意,您的程序逻辑可能存在缺陷……您逐一浏览CSV的每一行,检查一个人的名字。 That means that if you have more than one entry in the CSV file, (ie - the CSV file holds more than just one person's info), you're only really able to access the first in the list. 这意味着,如果CSV文件中有多个条目(即-CSV文件包含的信息不止一个人),则您实际上只能访问列表中的第一个条目。 You should probably prompt for a username, THEN run through each row of the CSV to check for a match. 您可能应该提示输入用户名,然后遍历CSV的每一行以检查是否匹配。 Only if there is no match in the entire CSV should you ask for another name... Just give it a look... 仅当整个CSV文件中都没有匹配项时,您才需要输入其他名称...看看吧...

If you are actually interested in other solutions go for my answer else just skip it. 如果您实际上对其他解决方案感兴趣,请直接回答。 With pandas it is more easier to solve your need. 使用熊猫,可以更轻松地解决您的需求。

So I have a CSV file("names.csv") with this values in it, 所以我有一个CSV文件(“ names.csv”),其中包含此值,

Name    Hours
Sri Mati    1
Some Name   2

Here is the code: 这是代码:

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")

Sample Input and Output: 样本输入和输出:

Enter your name: Steve Miller
Name not there

Next run, 下一轮

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]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM