【问题标题】:Looping a single sentence by a user - entered integer用户输入的整数循环单个句子
【发布时间】:2016-04-25 10:00:43
【问题描述】:

所以我的计算机科学老师给了我一个问题,但我已经坐在这里很久了,我一生都无法弄清楚。问题中有很多任务,但只有一个我遇到了麻烦。

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

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

如果有人能解决这个问题,他们正在帮助我加载。谢谢:)

【问题讨论】:

  • 你想要numberOfRooms = int(input("blah blah")) 而不是numberOfRooms = input(int("blah blah"))。您正在尝试将您的消息作为整数转换为用户,但实际上您希望将用户的响应转换为整数。
  • 这是一个很好的例子,说明查看确切的错误输出可以提供信息。我猜你会得到一个ValueError,但最好不要让我们猜测,通过添加确切的错误输出,它可以更容易地回答你的问题。查看stackoverflow.com/help/mcve 了解如何改进写作问题的信息。
  • 我会,但代码没有有用的错误,因为我问的是如何继续它,而不是它有什么问题。不过还是谢谢:)

标签: python loops variables integer int


【解决方案1】:

也许使用 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 计算课程做类似的任务;)祝你好运!

【讨论】:

  • 谢谢老兄,成功了!祝你的课程好运,我也很快开始我的课程了,我在 Edexcel
猜你喜欢
  • 1970-01-01
  • 2021-09-09
  • 2020-08-15
  • 2019-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-17
相关资源
最近更新 更多