简体   繁体   English

Python 使用 do while 循环的出勤率计算器

[英]Python attendance percentage calculator using do while loop

The aim is to create a percentage attendace calculator based on a given list of names.目的是根据给定的姓名列表创建一个出席百分比计算器。 I was able to loop the input questions with the names in the list.我能够使用列表中的名称循环输入问题。 However my issues lie with the following if condition which should output a 1 for each input question if the answer is y for each person in the list or a 0 if the answer n for any person in the list.但是,我的问题在于以下 if 条件,如果列表中每个人的答案为 y,则 output 对于每个输入问题应为 1,如果列表中任何人的答案为 n,则应为 0。 The number outputs should then be added in order to obtain a precentage.然后应该添加数字输出以获得百分比。 Please help.请帮忙。

continueLooping=True

students=["Annie", "Brian", "Clare", "Danny", "Ellen"]

i=0

while i<len(students):
    askuser=input(f"Is {students[i]} present?[y/n]")
    i=i+1

    if askuser == "y":
        one = 1
        print("1")

    else:
        one = -1
        print("-1")

        while one<len(students):
            if askuser == "y":
               one = 1
                one=one+1
            else:
                one = -1
                one=one+1

percentage=(one)/len(students)
print(percentage)

Your second inner while loop will terminate only if the first answer is "y".只有当第一个答案是“y”时,您的第二个内部 while 循环才会终止。

What you should be aiming to do is count the number of "y" answers you receive in the outer while loop.您应该打算做的是计算您在外部 while 循环中收到的“y”答案的数量。 As such:像这样:

students=["Annie", "Brian", "Clare", "Danny", "Ellen"]

i=0
studentsPresentCount = 0

while i<len(students):
    askuser=input(f"Is {students[i]} present?[y/n]")
    i=i+1

    if askuser == "y":
        studentsPresentCount += 1




percentage = studentsPresentCount/len(students)
print(percentage)

Note you can use the syntactic sugar 'i += 1' instead of 'i = i + 1'.请注意,您可以使用语法糖“i += 1”而不是“i = i + 1”。

In trying to stay as close to your code as possible, the code below works for what you describe.在尝试尽可能接近您的代码时,下面的代码适用于您所描述的内容。

There are three main corrections to the code:对代码进行了三个主要更正:

  1. You have an inner while loop that isn't necessary.您有一个不必要的内部 while 循环。 On each pass of the outer while loop, you can ask about each student, output the desired attendance, and adjust the attendance score.在外层while循环的每一次通过时,您可以询问每个学生的出勤率,并调整出勤率。 There's no need for a second loop inside.内部不需要第二个循环。

  2. You keep changing the value of "one", rather than incrementing or decrementing.您不断更改“一”的值,而不是递增或递减。 You can set one to 0 outside the loop (as you do with your counter i), and adjust it inside the loop.您可以在循环外将 1 设置为 0(就像使用计数器 i 一样),并在循环内对其进行调整。

  3. You probably don't want non-attendance to be -1, but rather have it as zero.您可能不希望缺勤为 -1,而是将其设为零。 Imagine a scenario with 3 attendees and 2 non-attendees.想象一个有 3 名与会者和 2 名非与会者的场景。 Your scoring would end up with 1+1+1-1-1, for an attendance score of 1 - when in fact the attendance score is 3.你的得分最终会是 1+1+1-1-1,出勤分数为 1 - 实际上出勤分数是 3。

There's also no need for the "continueLooping" variable at the outset.一开始也不需要“continueLooping”变量。

Last point, if you want percent, you can multiply the percentage value by 100 (as it is, it returns the decimal value).最后一点,如果你想要百分比,你可以将百分比值乘以 100(实际上,它返回十进制值)。

The working code, with these corrections, is:经过这些更正的工作代码是:

students=["Annie", "Brian", "Clare", "Danny", "Ellen"]

i=0
one = 0

while i<len(students):
    askuser=input(f"Is {students[i]} present?[y/n]")
    i=i+1

    if askuser == "y":
        one += 1
        print("1")

    else:
        print("0")



percentage=(one)/len(students)
print(percentage)

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

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