python新手需要使用while循环的帮助吗

2024-10-04 11:25:59 发布

您现在位置:Python中文网/ 问答频道 /正文

作业:

用Python编写一个交互式应用程序来显示 简单的菜单给用户。让他们选择一个男孩(b)、女孩(g)、或者退出(q) 这个计划。程序应该一直循环,直到用户选择退出。 此应用程序将使用循环和条件来完成任务。你的程序应该输出平均男孩分数和平均女孩分数。你知道吗

代码

 letter= input(" type (b) for Boy (g) for Girl or (q) for quit")
 boycount= 0
 girlcount=0


while(letter != 'q'):
   if  letter == 'b':
       print("Enter score for boy")
       scoreB= float(input())
       boycount = boycount +1
       letter=input(" type (b) for Boy (g) for Girl or (q) for quit")

if letter == 'g':
    print("enter score fo Girl")
    scoreG = float(input())
    girlcount= girlcount +1
    letter=input(" type (b) for Boy (g) for Girl or (q) for quit")

else:

   print("the average for the girls is",scoreG/girlcount)
   print("the average for the boys is",scoreB/boycount)

作为python的新手,我不知道该怎么做。我知道我需要做什么和得到的错误消息,但在python中实现它是我陷入困境的地方。你知道吗

错误我得到:在为b输入一个值并尝试为b输入另一个值之后,我得到一个错误,表示scoreG未定义


Tags: orthe应用程序forinputtype错误quit
2条回答

这确实是一个缩进问题(或逻辑流问题)。对于大多数编程语言来说,这无关紧要,但在python中却是必不可少的。你知道吗

下面是您的代码实际执行的操作:

# initialize boycount and girlcount
while(letter != 'q'):
    # do some stuff inside the while loop
    # do some stuff inside the while loop
    if letter == 'b':
        # increment boycount

    # do some stuff inside the while loop
# do some stuff after the while loop

# letter must be 'q' now that the while loop ended
if letter == 'g':
    # increment girlcount
    # this will never happen, because letter cannot be 'g' here
else:
    # print summary
    # this will always happen because letter is 'q'

下面是代码应该执行的操作:

# initialize boycount and girlcount
while(letter != 'q'):
    # do some stuff inside the while loop
    # do some stuff inside the while loop
    if letter == 'b':
        # increment boycount
    if letter == 'g':
        # increment girlcount
    # do some stuff inside the while loop
# do some stuff after the while loop
# letter must be 'q' now that the while loop ended
# print summary

与大多数其他编程语言不同,python需要缩进来定义复合语句块的范围。我有时会用一个donothingpass语句和一个注释来表示我的意图:

# initialize boycount and girlcount
while(letter != 'q'):
    # do some stuff inside the while loop
    if letter == 'b':
        # increment boycount
    pass # end if
    if letter == 'g':
        # increment girlcount
    pass # end if
pass # end while
# print summary

pass语句只是一个不起任何作用的占位符,但它可以帮助我使预期的控制流更清晰,并且可以帮助我检测设计错误。你知道吗

另一个可以使用的诊断工具是print()语句。你知道吗

# initialize boycount and girlcount
print("start of while letter loop")
while(letter != 'q'):
    # do some stuff inside the while loop
    if letter == 'b':
        print("letter b: increment boycount")
        # increment boycount
    pass # end if
    if letter == 'g':
        print("letter g: increment girlcount")
        # increment girlcount
    pass # end if
pass # end while
print("end of while letter loop")
# print summary

当您测试包含这些print语句的程序时,您将能够确认每个命令都按预期执行。在验证了逻辑工作之后,您只需在print语句前面放置一个#,将其转换为注释。你知道吗

为了完整起见,官方python tutorial提到

The body of the loop is indented: indentation is Python’s way of grouping statements

实际上,最大的问题是压痕。你知道吗

这应该起作用:

letter= input(" type (b) for Boy (g) for Girl or (q) for quit")
boycount= 0
girlcount=0
scoreB = 0
scoreG = 0

while True:
   if  letter == 'b':
       print("Enter score for boy")
       scoreB += float(input())
       boycount = boycount +1
       letter=input(" type (b) for Boy (g) for Girl or (q) for quit")
   elif letter == 'g':
       print("enter score fo Girl")
       scoreG += float(input())
       girlcount= girlcount +1
       letter=input(" type (b) for Boy (g) for Girl or (q) for quit")
   elif letter == 'q':
       print("the average for the girls is",scoreG/girlcount)
       print("the average for the boys is",scoreB/boycount)
       exit()

相关问题 更多 >