简体   繁体   English

卡在python中的while循环中

[英]Stuck in a while loop in python

I'm very new to python so bear with me. 我是python的新手,所以请多多包涵。 I have this code below which I've inserted a while loop to make sure every time a name is given it checks to verify that name is in the csv and then if it isn't it gives and error and asks for a new name. 我在下面的代码中插入了一个while循环,以确保每次给定名称时,它都会检查以确保该名称在csv中,如果不是,则会给出错误提示并输入新名称。 My problem is that I can't seem to get out of the loop. 我的问题是我似乎无法摆脱困境。 Even with a new name that I know for a fact is in the csv it still sticks in the loop. 即使我知道一个新名称在csv中,它仍然会陷入循环。 My question is how can I get this code to run through a loop and verify the name is in there? 我的问题是如何获取此代码以循环运行并验证名称是否在其中? any help would be great. 任何帮助都会很棒。 I am even open to changing the entire code if there's a better way to write this. 如果有更好的编写方式,我什至愿意更改整个代码。

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

You're asking for input inside the while loop that never checks any row other then the current row. 您要在while循环中要求输入,该输入从不检查除当前行以外的任何行。 It looks like you're trying to iterate over the entire list to see if the name is anywhere in there (which is rough performance wise but I won't confuse you by getting into that xD) so your input check and loops need to be moved around a bit like so: 看起来您正在尝试遍历整个列表以查看名称是否在其中(这在粗略的性能上是明智的选择,但我不会因为进入xD而感到困惑),因此您需要进行输入检查和循环像这样移动了一下:

import csv

with open('Report1.csv') as csvfile:
  hour_summation = {}
  read_csv = csv.reader(csvfile, delimiter=',')
  name_found = False
  while not name_found:
    # take user input for name
    full_name = input('Enter your full name: ').lower()
    # start search at top of file. Have to do this or each loop will
    # start at the end of the file after the first check.
    csvfile.seek(0)
    for row in read_csv:
      # check to see if row matches name
      if(' '.join((row[0], row[1]))).lower() == full_name.strip().lower():
        name_found = True
        hour_summation[row[2]] = hour_summation.get(row[2], 0) + int(float(row[3]))

    # name wasn't found, so repeat process
    if not name_found:
      # name wasn't found, go back to start of while loop
      print('Name is not in system')
    else:
      # break out of while loop. Technically not required since while
      # will exit here anyway, but clarity is nice
      break

print('This is {} full hours report:'.format(full_name))
for k, v in hour_summation.items():
    print(k + ': ' + str(v) + ' hours')
import csv

full_name = input('Enter your full name: ').lower()
bucle = True
with open('Report1.csv') as csvfile:
    hour_summation = {}
    read_csv = csv.reader(csvfile, delimiter=',')
    while bucle:    
        for row in read_csv:
            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]))
                bucle = False
        if bucle:
            print('Name is not in system')
            full_name = input('Enter your full name: ').lower()
            csvfile.seek(0)

print('This is {} full hours report:'.format(full_name))
for k, v in hour_summation.items():
print(k + ': ' + str(v) + ' hours')

assuming the file can have multiple lines related to the given full name, try something like: 假设文件可以有多行与给定的全名相关,请尝试如下操作:

import csv

full_name = input('Enter your full name: ').lower()
run=1
while run:    
    with open('Report1.csv') as csvfile:
        hour_summation = {}
        read_csv = csv.reader(csvfile, delimiter=',')
        for row in read_csv:
            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]))

    if hour_summation:
        print('This is {} full hours report:'.format(full_name))
        for k, v in hour_summation.items():
            print(k + ': ' + str(v) + ' hours')
        run=0
    else:
        print('Name is not in system')
        full_name = input('Enter your full name: ').lower() # following your code, 
                                                            # request for a new full name comes only in case no results for the 1st one 

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

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