在Python3中乘以循环输入

时间:2018-11-30 15:55:46

标签: python list for-loop int

我正在用Python3编写一个计算网络可用性的小程序。

根据我的数据通信课程,您可以通过乘以网络中每个设备的可用性来确定网络可用性。

例如:您的网络中有3台设备。设备1的可用性为0.67。设备2的可用性为0.94。设备3的可用性为.79。然后,您将可用性乘以:.67 * .94 * .79 = .498网络可用性。

到目前为止,这是我的代码:

# Network Availability
# by Nicholas Zachariah

numDev = int(input("How many devices do you have? ")) # number of devices
print(f"There are {numDev} devices.")
devList = list(range(1, numDev+1)) # device list


for device in devList:
    ava = input(f"What is the availability of device number {device}? ") # availability

在这里,我想存储每个可用性输入,然后将每个设备的可用性相乘并打印整个网络的可用性,但是由于无法弄清楚如何单独存储每个设备的可用性,因此我在执行此任务时遇到了麻烦。有人可以帮忙吗?

PS 简而言之,我正在寻找网络的总可用性。

5 个答案:

答案 0 :(得分:1)

第一件事是您要根据给定的输入定义ava。在这种情况下,给定的输入应该是一个int,所以ava将是一个int。您需要预先指定ava是一个列表,以便可以使用.append()附加输入值。

之后,您可以遍历ava列表并乘以结果:

# Network Availability
# by Nicholas Zachariah

numDev = int(input("How many devices do you have? ")) # number of devices
print(f"There are {numDev} devices.")
devList = list(range(1, numDev+1)) # device list
ava = []

for device in devList:
    ava.append(int(input(f"What is the availability of device number {device}? ")))

print (ava)
TotalAvailability = 1

for device in ava:
    TotalAvailability *=device

print( TotalAvailability)   

答案 1 :(得分:1)

使用dict作为值。

#!/usr/bin/python3.5
import operator
from functools import reduce

numDev = int(input("How many devices do you have? ")) # number of devices
print("There are {} devices.".format(numDev))

devList = list(range(1, numDev+1)) # device list
ava = {}

for device in devList:
    ava[device] = float(input("What is the availability of device number {}? ".format(device))) #add to dict

print("{:.2f}".format(reduce(operator.mul, ava.values(), 1)))

>>>How many devices do you have? 2
>>>There are 2 devices.
>>>What is the availability of device number 1? .65
>>>What is the availability of device number 2? .66
>>>0.43

答案 2 :(得分:1)

根据我的理解(不是其他所有人的推断),您想要找到网络可用性,即所有可用性的乘积。

为了单独存储可用性,可以在python中使用许多可迭代对象之一,最简单的是list。列表是一种数据结构,其中包含多个元素(不一定是同一类型)。

为了实现您想要实现的目标,您需要满足以下条件:

# Network Availability
# by Nicholas Zachariah

numDev = int(input("How many devices do you have? ")) # number of devices
print(f"There are {numDev} devices.")
devList = list(range(1, numDev+1)) # device list

availability_list = list()
for device in devList:
    ava = input(f"What is the availability of device number {device}? ") # availability
    availability_list.append(ava)
curr_avail = availability_list.pop()
for avail in availability_list:
    curr_avail = curr_avail*avail

例如,当您输入0.8、0.7和0.6

availability_list = [0.8, 0.7, 0.6]
curr_avail = availability_list.pop()
for avail in availability_list:
    curr_avail = curr_avail*avail

curr_avail
>>> 0.33599999999999997

答案 3 :(得分:0)

迈克尔·金(Michael King)有一个很好的答案,我将其标记为正确。这是基于他的回答的新代码。我只是对其进行了一些整理,并使用了舍入功能,因此最终结果没有十位小数。

netAva = 1 # Network Availability
ava = [] # List of each device's availability

numDev = int(input("How many devices do you have? ")) # number of devices
print(f"There are {numDev} devices.")
devList = list(range(1, numDev+1)) # device list

for device in devList:
    ava.append(float(input(f"What is the availability of device number     {device}? "))) # availability

for device in ava:
    netAva *= device

netAva = round(netAva, 3)

print(f"{netAva} is your network's availability.")

答案 4 :(得分:-1)

您可以在for循环添加使用list.append()

之前创建一个列表。
numDev = int(input('How many devices do you have? '))
print(f'There are {numDev} devices.')
devList = list(range(1, numDev+1))
avaList = []

for device in devlist:
    avaList.append(input(f'What is the availability of device number {device}? '))