Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我需要这个循环,以允许3人输入信息,然后打印出与列表的信息_Python - Fatal编程技术网

Python 我需要这个循环,以允许3人输入信息,然后打印出与列表的信息

Python 我需要这个循环,以允许3人输入信息,然后打印出与列表的信息,python,Python,代码有几个问题,主要问题是楼层编号是字符串,不能与整数进行比较。这就是为什么你的while循环永远不会结束。在下面的代码中,我没有使用多个列表,而是使用一个字典来跟踪数据。字典键是租户名称,值是列表。列表中的第一项是楼层号,第二项是该租户的月租金。最后,您将遍历此字典以打印适当的输出 Floor Monthly Rent 1 to 3 $1200 4 to 6 $ 1500 7 to 9 $2000 10 $3500 TEST EXAMPLES OF CODE Custo

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

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”,这就是标记的用途。代码中有一些问题需要解决。作为第一个建议,我建议检查哪里需要数字(int)和哪里需要字符串(str)。建议:
input
函数返回字符串(str)作为输出,而不是数字(int)。Stefano,我需要将“str”和“int”放在哪里?我对python非常陌生,也不熟悉。
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
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