编码问题的条件逻辑?

时间:2016-03-12 13:29:08

标签: python

如果用户输入了' N'不,你怎么避免50美元的额外费用,并跳过要求高度和宽度来计算表面积,并继续其余的代码?请记住,我没有在代码之后写下问题;如果用户键入了' Y'如果是,那么将添加50美元,程序将询问房间内每面墙的宽度和高度。

total_surface_area = 0
number = int(input("Number of rooms to paint")
print("\n***ROOM DETAILS***")
for room in range(number):
    room_name = input("\n\nPlease enter the name of the room:")
    wall_number = int(input("Number of walls in the room:"))
    wallpaper = input("\nWould you like to remove the paint? (Y for Yes/N for No). If yes, $50 will be charged per room:")

2 个答案:

答案 0 :(得分:0)

对于我们来说,确定问题的最佳解决方案是什么,我可能有点过于模糊,但我怀疑您正在寻找continuebreak语句。您可以找到解释here

这可以说明如何使用这两种语句:

total_surface_area = 0
# You were missing a parenthesis in the following line.
number = int(input("Number of rooms to paint"))
print("\n***ROOM DETAILS***")
for room in range(number):
    print('=== details of room number '+str(room)+' ===')
    room_name = input("\n\nPlease enter the name of the room:")
    wall_number = int(input("Number of walls in the room:"))

    print("\nWould you like to remove the paint? (Y for Yes/N for No). If yes, $50 will be charged per room:")
    while True:
        buffer = input()
        if buffer == 'Y':
            wallpaper = True
            break
        elif buffer == 'N':
            wallpaper = False
            break
        else:
            print('please anser "Y" or "N"')

    if wallpaper:
        print('I will now ask many more questions.......')
        # continue asking questions ...
    else:
        # move to the next room
        continue

答案 1 :(得分:0)

这可能就是你要找的东西:

from distutils.util import strtobool
total_surface_area = 0
extra_cost = 0
number = int(input("Number of rooms to paint")
print("\n***ROOM DETAILS***")
for room in range(number):
    room_name = input("\n\nPlease enter the name of the room:")
    wall_number = int(input("Number of walls in the room:"))
    wallpaper = input("\nWould you like to remove the paint? (Y for Yes/N for No). If yes, $50 will be charged per room:")
    if strtobool(wallpaper):
        extra_cost += 50
        dimensions = []
        for wall in range(wall_number):
            dimensions.append(input("Please input wall dimensions..."))

    ...