简体   繁体   中英

Looping a single sentence by a user - entered integer

So I was given a problem by my Computer Science teacher, but I have been sat here for ages and cannot for the life of me figure it out. There are plenty of tasks within the problem, but there is only one I'm having trouble with.

So, I am coding a program for a carpet shop, and the part that I am stuck on is this.

2: For each floor you need to carpet, ask the user for the name of the room that the floor is located in. Basically X number of rooms; need to loop a sentence asking for the name of the room multiplied by X. So if the user has 3 rooms they need carpeted, I would end up with the name of 3 rooms (Lounge, Bedroom etc.), and be able to display these results back to the user later. Here is what I have so far...

#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??

If someone can work this out they are helping me loads. Thanks :)

Perhaps using a for loop perhaps?

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

Full code:

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]))

Btw I'm doing a similar task for my OCR computing coursework ;) so good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM