用户循环一个句子 - 输入整数

时间:2016-01-19 19:13:27

标签: python loops variables integer int

所以我的计算机科学老师给我一个问题,但是我已经在这里待了好几年了,不能为我的生活弄清楚。问题中有很多任务,但只有一个我遇到了麻烦。

所以,我正在编写一个地毯商店的程序,而我所坚持的部分就是这个。

2:对于每个需要铺地毯的楼层,请询问用户楼层所在房间的名称。 基本上是X个房间;需要循环一个句子,要求房间的名称乘以X.所以如果用户有3个房间,他们需要铺地毯,我最终会得到3个房间的名称(休息室,卧室等),并能够显示这些结果稍后会回复给用户。这是我到目前为止所拥有的......

#Generating An Estimate. 

#Welcome message + setting base price to 0 + defining "getting_estimate" variable.
def getting_estimate ():
    overallPrice = 0
    print("Welcome!")

#Getting the information about the customer.
    custID = input ("\n1. Please enter the customer's ID: ")
    estimateDate = input ("\n2. Please enter the date: ")
    numberOfRooms = input (int("\n3. Please enter the number of rooms that need to be carpeted: "))

#Add input allowing user to enter the 
#name of the room, looped by the integer entered
#in the Variable "numberOfRooms", and possibly
#adding 1st room, 2nd room etc??

如果有人可以解决这个问题,他们会帮助我。谢谢:))

1 个答案:

答案 0 :(得分:0)

也许使用for循环?

    for i in range(numberOfrooms):
        roomName = input("Blah blah")

完整代码:

def getting_estimate ():
overallPrice = 0
print("Welcome!")

custID = input ("\n1. Please enter the customer's ID: ")
estimateDate = input ("\n2. Please enter the date: ")
numberOfRooms = int (input("\n3. Please enter the number of rooms that need to be carpeted: "))
cust_roster = {custID:[]}
for i in range(numberOfRooms):
    roomName = input("Enter the name of room number "+str(i+1))
    cust_roster[custID].append(roomName)
print("{} has {} rooms called: {}".format(custID,
                                          len(cust_roster[custID]),
                                          cust_roster[custID]))

顺便说一下,我为OCR计算课程做了类似的工作;)祝你好运!