【问题标题】:Keeping a running total of score in python 3在python 3中保持总分
【发布时间】:2018-04-05 12:18:24
【问题描述】:

我正在编写一个掷两个骰子的程序;然后根据滚动的内容分配积分;并保留运行总计。到目前为止,我有这个;但我一直遇到“int is not callable”的错误。有人可以帮忙吗?

import random
def dice():
    a = 1
    b = 6
    return random.randint(a,b)
rollOne = int(dice())
rollTwo = int(dice())


def greeting():
    option = input('Enter Y if you would like to roll the dice: ')
    if option == 'Y':
       print('You have rolled a' , rollOne, 'and a' , rollTwo)
       points = []

       if rollOne() == rollTwo():

           points.append(10)

           print('You have a total of %d points' % (sum(points)))

       if rollOne == 6 or rollTwo ==6:

           points.append(4)

           print('You have a total of %d points' % (sum(points)))

       if (rollOne + rollTwo) == 7:

           points.append(2)

           print('You have a total of %d points' % (sum(points)))

dice()
greeting()

【问题讨论】:

  • 去掉if rollOne() == rollTwo():的括号
  • 请注意,对于一对六,您将获得 10 分(因为相同),然后是 4 分,因为两者都是六。不确定这是否是故意的......

标签: python python-3.x random dice cumulative-sum


【解决方案1】:

dice() 的结果是一个整数,您将其命名为 rollOnerollTwo

它们不能像您尝试做的那样被“调用”rollOne()

为了解决错误,请从行中删除括号(您在其他 if 语句中已经这样做了)

if rollOne() == rollTwo(): 

变成

if rollOne == rollTwo: 

【讨论】:

    【解决方案2】:

    问题出在这,

       if rollOne() == rollTwo():
    

    rollone 和 rolltwo 是返回值而不是函数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-01
      • 1970-01-01
      • 2013-10-01
      • 2014-12-13
      • 1970-01-01
      • 2014-02-20
      • 2011-10-04
      • 1970-01-01
      相关资源
      最近更新 更多