【问题标题】:Starting out with python从 python 开始
【发布时间】:2017-08-14 13:36:49
【问题描述】:

我今天刚开始使用 python,我已经看到了与其他编程语言的一些差异。

我无法让它工作。

我有一个余额需要在调用存款函数时提高,但它保留的值是它给出的最后一个值。

        balance = 0

        counter = 0



    def deposit(amount):
        global balance
        balance = balance + amount
        counter += 1
        print (balance + counter)




deposit (1000)


deposit (10100)

【问题讨论】:

  • 确保在发布 Python 代码时准确地复制缩进。否则,您将在代码中引入新错误。
  • define "I can't get this to work." 它是否无法编译、是否崩溃、是否给您带来意想不到的结果?

标签: python


【解决方案1】:

试试:

balance = 0
counter = 0

def deposit(amount):
    global balance, counter
    balance = balance + amount
    counter += 1
    print (balance + counter)

deposit (1000)

deposit (10100)

输出:

1001
11102

【讨论】:

  • 你可以补充说global的使用被认为是不好的风格。
猜你喜欢
  • 2017-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-21
  • 2017-09-29
  • 1970-01-01
相关资源
最近更新 更多