首页 > 解决方案 > 我需要这个循环来允许 3 个人输入信息,然后打印出带有列表的信息

问题描述

    #identify the variables

floor_number = 0
rent_cost = 0
total_floors = 0
Floors = []
Rent = []
Names = []
a = 0

#begin while loop for user information

while ( a < 2):
    name1 = input("Please enter your name: ")
    Names.append(name1)
    floor_number = input("Please enter your floor number: ")
    Floors.append(floor_number)
    floor_number += (str(total_floors))

    if (floor_number >= 1):
        print("Your rent is: $1200")
        rent_cost = "$1200"
     
    elif (floor_number > 3):
        print("Your rent is: $1500")
        rent_cost = "$1500"

    elif(floor_number > 6):
        print("Your rent is: $2000")
        rent_cost = "$2000"

    elif(floor_number > 9):
        print("Your rent is: $3500")
        rent_cost = "$3500"

        Rent.append(rent_cost)
        a += 1
#end while loop of user information

#begin while loop to print out user information

i = 0
while( i < len(Names)):
    print(Names[a], "-----", Rent[a])
    i += 1

#print out the average floor and highest floor that were entered in the program

print("The average floor is: ", total_floors/3)
print("The highest floor is: ", max(Floors))

问题表述如下:

公寓大楼根据公寓的楼层收取月租费。编写一个 python 程序,使用循环提示输入 3 个客户姓名和他们公寓的楼层。使用下表确定每个客户的月租金。您的程序还应确定 3 位客户的最高楼层和 3 位客户的平均楼层。该程序应显示每个客户的姓名和月租。同时输出3的平均楼层和3的最高楼层。编写Python代码显示Test Data结果

Floor

Monthly Rent

1 to 3

$1200

4 to 6

$ 1500

7 to 9

$2000

10

$3500

 TEST EXAMPLES OF CODE

Customer Name

Floor

Katia

5

Omar

2

Dominic

10

标签: python

解决方案


代码有几个问题,主要是楼层号码是一个字符串,不能与整数进行比较。这就是为什么你的 while 循环永远不会结束的原因。在下面的代码中,我没有多个列表,而是使用一个字典来跟踪数据。字典键是租户名称,值是列表。列表中的第一项是楼层号,第二项是该租户的月租金。最后,您遍历此字典以打印适当的输出。

dic = {} #key is name. Value is a list containing floor and rent.
a = 0
while (a < 3):
    name = input("Please enter your name: ")
    try: #make sure the user inputs a number
        floor_number = int(input("Please enter your floor number between 1 and 10: "))
    except ValueError:
        print("Invalid input.")

    if 1 <= floor_number <= 10:
        a += 1
        dic[name] = [floor_number]
        if 1 <= floor_number <= 3:
            dic[name].append(1200)
        elif 4 <= floor_number <= 6:
            dic[name].append(1500)
        elif 7 <= floor_number <= 9:
            dic[name].append(2000)
        else:
            dic[name].append(3500)
    else: #make sure it's a valid floor. Otherwise don't count that input and prompt user again.
        print('No such floor. Try again.')

#loop through dictionary to print output
floors = []
for name, value_list in dic.items():
    print(f'{name} lives on floor {value_list[0]} and pays ${value_list[1]} in monthly rent.')
    floors.append(value_list[0])

#calculate and print aggregate data
highest_floor = max(floors)
average_floor = int(sum(floors)/len(floors))
print(f'The highest floor is floor {highest_floor}. The average floor is {average_floor}')

样本输出:

Please enter your name: Katia
Please enter your floor number between 1 and 10: 5
Please enter your name: Omar
Please enter your floor number between 1 and 10: 2
Please enter your name: Dominic
Please enter your floor number between 1 and 10: 10

Katia lives on floor 5 and pays $1500 in monthly rent.
Omar lives on floor 2 and pays $1200 in monthly rent.
Dominic lives on floor 10 and pays $3500 in monthly rent.
The highest floor is floor 10. The average floor is 5

推荐阅读