分配:
在Python中编写一个交互式应用程序以显示 简单的菜单给用户。让他们选择一个男孩(b),女孩(g)或 quit(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
答案 0 :(得分:1)
实际上,最大的问题是缩进。
这应该有效:
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()
答案 1 :(得分:0)
这确实是一个缩进问题(或逻辑流问题)。对于大多数编程语言而言,这并不重要,但这在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需要缩进来定义复合语句块的范围。有时,我会在不做任何事的pass
语句中加注以表明我的意图:
# 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
在使用包含这些打印语句的程序进行测试时,您将能够确认每个命令是否达到了预期的效果。在验证了逻辑原理之后,您只需在打印语句前放置#
即可将其转换为注释。
出于完整性考虑,python tutorial官方提到
循环的主体是缩进的:缩进是Python的处理方式 分组语句