Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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';对于';循环迭代数组_Python_Loops - Fatal编程技术网

Python';对于';循环迭代数组

Python';对于';循环迭代数组,python,loops,Python,Loops,我是python新手,我正在寻找一些关于循环结构的帮助,特别是如何使用“for”循环或替代循环来解决问题。我需要根据数组中前几天的折扣价格计算折扣价格,这样它将遍历数组并应用10%的折扣。以下是我到目前为止的情况: opnDays = ["mon", "tue", "wed", "thr", "fri"] price = 10 def discount(array): for day in array: disPrice = price - (price * .1)

我是python新手,我正在寻找一些关于循环结构的帮助,特别是如何使用“for”循环或替代循环来解决问题。我需要根据数组中前几天的折扣价格计算折扣价格,这样它将遍历数组并应用10%的折扣。以下是我到目前为止的情况:

opnDays = ["mon", "tue", "wed", "thr", "fri"]

price = 10

def discount(array):
    for day in array:
        disPrice = price - (price * .1)
        print(day, disPrice)
discount(opnDays)
>>>
mon 9.0
tue 9.0
wed 9.0
thr 9.0
fri 9.0
我想得到:

>>>
mon 9.0
tue 8.1
wed 7.29
thr 6.561
fri 5.9109
任何关于如何组织此循环以获得前一天10%折扣的帮助都会很有帮助。
谢谢

您没有更改
price
的值,因此循环的每次迭代都会计算相同的结果

这应该起作用:

for day in array:
    price = price - (price * .1)
    print(day, price)

您编写的代码已关闭:

opnDays = ["mon", "tue", "wed", "thr", "fri"]

price = 10

def discount(array):
    disPrice = price
    for day in array:
        disPrice *= 0.9
        print(day, disPrice)

我在这里所做的是改变
disPrice
在循环中的设置方式。您在每次迭代中将其设置为相同的值(
0.9*price
)。我所做的只是在开始时将其设置为
price
,并在每次迭代时将其乘以
0.9
,从而产生所需的行为。

如果您想坚持在代码中使用函数,以下是一种方法:

opnDays = ["mon", "tue", "wed", "thr", "fri"]

price = 10

def discount(array):
    disPrice = price # assign initial price to discount
    for day in array:
        disPrice = disPrice*0.9 # Reduce the discount by 10 percent every time
        print(day, disPrice)

discount(opnDays)
输出

mon 9.0
tue 8.1
wed 7.29
thr 6.561
fri 5.9049000000000005
mon: 9.00
tue: 8.10
wed: 7.29
thr: 6.56
fri: 5.90

您可以使用全局并保持逻辑不变

opnDays = ["mon", "tue", "wed", "thr", "fri"]

price = 10
def discount(array):

    global price

    for day in array:
        price = price - (price * .1)
        print(day, price)
discount(opnDays)
为了避免
global
将价格作为参数和首选方法传递

opnDays = ["mon", "tue", "wed", "thr", "fri"]

price = 10
def discount(array,price):

    for day in array:
        price = price - (price * .1)
        print(day, round(price,2))
discount(opnDays,price)
如果您需要连续迭代后的最终价格,我相信这可能是您的要求

opnDays = ["mon", "tue", "wed", "thr", "fri"]

price = 10
def discount(array,price):

    for day in array:
        price = price - (price * .1)
        print(day, round(price,2))
    return price
final_price = discount(opnDays,price)
print(final_price)

您的代码非常接近,但每天都需要修改前一天的价格

我会将起始价格传递到函数中,就像您传递日期名称列表一样。我还使用了
.format
,因此我们可以将价格打印为美元和美分。这只会影响价格的打印方式,不会影响实际价值,因此我们不会失去任何准确性

我还稍微更改了您的名称,以便它们符合通常的Python样式

输出

mon 9.0
tue 8.1
wed 7.29
thr 6.561
fri 5.9049000000000005
mon: 9.00
tue: 8.10
wed: 7.29
thr: 6.56
fri: 5.90

price=price-(price*.1);打印(天,价格)
“我需要根据数组中前几天的折扣价格计算折扣价格”。好的,但是代码只是使用一个数组(实际上,它是一个列表)作为日名称,但是听起来你也应该使用一个数组作为折扣价格。正如下面的答案所示,你实际上并不需要它,但这个练习的重点可能是创建一个包含每天折扣价格的列表。@JoranBeasley:如果在函数中定义了价格,或者价格通过一个变量(也称为price)传递给函数,或者您必须在函数中定义全局价格,那么您的答案就行了。@Bazingaa:)谢谢您的帮助澄清:)这究竟是为什么被否决?Jay问了一个简单明了的问题,并向我们展示了他的代码,以及预期输出和实际输出。最好使用
price=price*0.9
Gives
UnboundLocalError:分配前引用的局部变量'price'。如果我在函数内部和for循环之前使用
global price
作为下面的答案之一,则works@nosklo:我知道,这是为了替换OP的for循环。但是,除非我在函数中定义了全局价格,否则我会得到这个UnboundLocalError。您可以使用
global
来实现这一点,但不应该鼓励这样做。可修改的全局变量破坏了程序模块化。对于这样一个小程序来说没什么大不了的,但这是一个坏习惯。我通常不喜欢使用global,但在这种情况下,我相信最终价格应该是迭代的结果,因为它将比原始价格降低7倍,并且应该能够在以后消费。@mad\ux:如果我在没有
全局价格的情况下尝试代码
,我得到了
UnboundLocalError:赋值前引用的局部变量'price'
@Bazingaa显然函数中没有声明price是的,因此只使用
price=price-(price*.1)
而不将价格传递给函数的解决方案将不起作用。这就是我要说的,非常有用。欣赏这种洞察力。