作业:
用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未定义
这确实是一个缩进问题(或逻辑流问题)。对于大多数编程语言来说,这无关紧要,但在python中却是必不可少的。你知道吗
下面是您的代码实际执行的操作:
下面是代码应该执行的操作:
与大多数其他编程语言不同,python需要缩进来定义复合语句块的范围。我有时会用一个donothing
pass
语句和一个注释来表示我的意图:pass
语句只是一个不起任何作用的占位符,但它可以帮助我使预期的控制流更清晰,并且可以帮助我检测设计错误。你知道吗另一个可以使用的诊断工具是print()语句。你知道吗
当您测试包含这些print语句的程序时,您将能够确认每个命令都按预期执行。在验证了逻辑工作之后,您只需在print语句前面放置一个
#
,将其转换为注释。你知道吗为了完整起见,官方python tutorial提到
实际上,最大的问题是压痕。你知道吗
这应该起作用:
相关问题 更多 >
编程相关推荐