如何多次遍历raw_input?

时间:2015-03-05 13:11:46

标签: python list raw-input

对于我的客户报价系统,我已经用raw_input,if,elif,else语句和数学方程编写了几个代码块来计算出一个房间的地毯价格。 现在,如果客户写入raw_input 3个房间,我希望包含50%的折扣,并且50%的折扣打折到三个房间中最便宜的房间。如果他们想要完成4个房间,他们可以免费获得最便宜的房间。

然而,我的困境是,循环问题的代码是什么?#34;长度是多长?"和"什么是宽度"与问题相同的次数"那里有多少房间"。所以基本上我希望代码询问他们放置的房间数量的长度和宽度问题。 最后,当谈到打折它们时,我如何得到(包括测量的房间)最低价格的房间和一半的价格。就像在,代码存储在哪里,以便能够检索它并对其进行折扣。

谢谢,如果你能待这么久。我的代码到目前为止,下面。

NumberOfRooms = int(raw_input("How many rooms are you looking to carpet? "))

LengthOfRoom = int(raw_input("What is the length of the room in Meters? "))

WidthOfRoom = int(raw_input("What is the width of the room in Meters? "))

areaOfRoom = LengthOfRoom * WidthOfRoom

CarpetType = raw_input("What type of carpet do you want? (Standard, Premium,            Executive) ")

print "The area of your room is " + str(areaOfRoom)
print "LengthOfRoom = %s, WidthOfRoom = %s, CarpetType = %s." %     (LengthOfRoom, WidthOfRoom, CarpetType)

Standard = 20
Premium = 30
Executive = 50

StandardRoomPrice = (areaOfRoom / 3.66) * Standard * NumberOfRooms
PremiumRoomPrice = (areaOfRoom / 3.66) * Premium * NumberOfRooms
ExecutiveRoomPrice = (areaOfRoom / 3.66) * Executive * NumberOfRooms
VolumeDiscount = 0.5

CarpetTyping = CarpetType

if CarpetTyping == "Standard":
    print StandardRoomPrice
elif CarpetTyping == "Premium":
    print PremiumRoomPrice
else:
print ExecutiveRoomPrice

2 个答案:

答案 0 :(得分:0)

您可以在范围num_rooms中循环获取宽度和高度:

from operator import mul

from itertools import count


prem_rate = 3.66
disc_rate = 1.83

while True:
    try:
       num_rooms = int(raw_input("How many rooms are you looking to carpet? "))
       break
    except ValueError:
        print("Please enter a number")

# use for dict keys for each room
cn = count(1)
room_measurements = {}

# validate and confirm input making sure we get a valid measurement for each room
while len(room_measurements) != num_rooms:
    print(room_measurements)
    try:
        m = map(int, raw_input("Enter length and width of room separated by a space i.e 12 10? ").split())
        # user entered something like 1010
        if len(m) != 2:
            print("Input must be in format width height")
            continue
        # make sure user is happy with sizes the entered
        confirm = raw_input("You entered width {} and height {}\n Press y to confirm or any key to try again".format(*m))
        if confirm.lower() != "y":
            continue
        room_measurements["room {}".format(next(cn))] = m
    # catch when user enters input that cannot be cast to int
    except ValueError:
        print("Invalid input")

# set disc_room to False initially
disc_room = False
if len(room_measurements) > 2:
    mn_size = min(room_measurements, key=lambda x: mul(*room_measurements[x]))
    disc_room = room_measurements[mn_size] 
    # remove room so we can calculate separately later
    del room_measurements[mn_size]

# if we get three or more rooms if disc_room will evaluate to True
# so calculate prem rate for larger rooms and add discounted room cost
if disc_room:
    total = sum(mul(*x) * prem_rate for x in room_measurements.itervalues()) + (mul(*disc_room) * disc_rate)
    print(total)
else:
    # else less than 3 rooms so just calculate prem rate
    total = sum(mul(*x) * prem_rate for x in room_measurements.itervalues())

处理您可能希望查看decimal模块的资金。

答案 1 :(得分:0)

首先,您需要存储用户输入,以便您了解客户的订购总数。我重写了一点代码(既不完美也不完整,但它应该让你开始)。找到最低价格的房间只是迭代订单并计算个别价格,但我会把它留给你作为练习。

orders = {}

num_rooms = int(raw_input("How many rooms are you looking to carpet? "))

for i in range(1, num_rooms + 1):
    order = {}
    print 'Room', i
    order['length'] = int(raw_input("What is the length of the room in Meters? "))
    order['width'] = int(raw_input("What is the width of the room in Meters? "))
    order['area'] = order['width'] * order['length']
    order['carpet_type'] = raw_input("What type of carpet do you want? ([S]tandard, [P]remium, [E]xecutive) ")
    orders[i] = order

ROOM_FACTORS = { 'S' : 20,
                'P' : 30,
                'E' : 50 }

total_price = 0.0
for order_id in orders:
    order = orders[order_id]
    total_price += order['area'] / 3.66 * ROOM_FACTORS[order['carpet_type']]

# Discount
if len(orders) > 2:
    total_price *= 0.5

print 'Total price is:', total_price

输出:

How many rooms are you looking to carpet? 2
Room 1
What is the length of the room in Meters? 1
What is the width of the room in Meters? 1
What type of carpet do you want? ([S]tandard, [P]remium, [E]xecutive) S
Room 2
What is the length of the room in Meters? 2
What is the width of the room in Meters? 2
What type of carpet do you want? ([S]tandard, [P]remium, [E]xecutive) P
Total price is: 38.2513661202