获取TypeError:缺少x必需的位置参数

时间:2015-12-09 04:42:31

标签: python

所以我在过去一小时左右一直在弄乱这段代码但是我无法在任何地方遇到不同的错误。我觉得这个版本与其他版本相比是最少的错误。

这是我的代码

#Variables
import random
count_2s = 0
count_3s = 0
count_4s = 0
count_5s = 0
count_6s = 0
count_7s = 0
count_8s = 0
count_9s = 0
count_10s = 0
count_11s = 0
count_12s = 0
user_input = 0
index = 0
die_1 = 0
die_2 = 0
dice_total = 0


#User Functions
def one():
    user_input = input('How many times to roll the dice?:')
    user_input = int(user_input)
    return user_input

def two():
    dice_total=0
    while index < user_input:
        die_1 = random.randint(1,6)
        die_2 = random.randint(1,6)
        dice_total = die_1 + die_2
    return dice_total, index

def three(dice_total, count_2s, count_3s, count_4s, count5s, count_6s, count_7s, count_8s, count_9s, count_10s, count_11s, count_12s, user_input):
    print("2's rolled: " , count_2s, "or %5.2f percent" , count_2s/user_input * 100)
    print("3's rolled: " , count_3s, "or %5.2f percent" , count_3s/user_input * 100)
    print("4's rolled: " , count_4s, "or %5.2f percent" , count_4s/user_input * 100)
    print("5's rolled: " , count_5s, "or %5.2f percent" , count_5s/user_input * 100)
    print("6's rolled: " , count_6s, "or %5.2f percent" , count_6s/user_input * 100)
    print("7's rolled: " , count_7s, "or %5.2f percent" , count_7s/user_input * 100)
    print("8's rolled: " , count_8s, "or %5.2f percent" , count_8s/user_input * 100)
    print("9's rolled: " , count_9s, "or %5.2f percent" , count_9s/user_input * 100)
    print("10's rolled: " , count_10s, "or %5.2f percent" , count_10s/user_input * 100)
    print("11's rolled: " , count_11s, "or %5.2f percent" , count_11s/user_input * 100)
    print("12's rolled: " , count_12s, "or %5.2f percent" , count_12s/user_input * 100)



def main(index):
    one()
    two()
    if dice_total == 2:
        count_2s = count_2s + 1
    elif dice_total == 3:
        count_3s = count_3s + 1
    elif dice_total == 4:
        count_4s = count_4s + 1
    elif dice_total == 5:
        count_5s = count_5s + 1
    elif dice_total == 6:
        count_6s = count_6s + 1
    elif dice_total == 7:
        count_7s = count_7s + 1
    elif dice_total == 8:
        count_8s = count_8s + 1
    elif dice_total == 9:
        count_9s = count_9s + 1
    elif dice_total == 10:
        count_10s = count_10s + 1
    elif dice_total == 11:
        count_11s = count_11s + 1
    elif dice_total == 12:
        count_12s = count_12s + 1
    index = index + 1
    three()

main(index)

这是我收到的错误:

How many times to roll the dice?:200
Traceback (most recent call last):
  File "C:/Users/Julian/Desktop/Python Stuff/lastone.py", line 78, in <module>
    main(index)
  File "C:/Users/Julian/Desktop/Python Stuff/lastone.py", line 76, in main
    three()
TypeError: three() missing 13 required positional arguments: 'dice_total', 'count_2s', 'count_3s', 'count_4s', 'count5s', 'count_6s', 'count_7s', 'count_8s', 'count_9s', 'count_10s', 'count_11s', 'count_12s', and 'user_input'

正确的方向任何一步都将不胜感激,谢谢! 我意识到一些步骤可能是用户定义的功能,但这只是必须如何完成。

1 个答案:

答案 0 :(得分:2)

这里有一些问题,让我们从顶部开始:

def one():
    user_input = input('How many times to roll the dice?:')
    user_input = int(user_input)
    return user_input

此方法返回一个值,但是当您调用它时,请在main()

中向下
def main(index):
    one()
    two()

您永远不会保存返回值。现在,您可能会认为,由于您在脚本的最顶部定义了user_input = 0one将使用您初始化的user_input

这不是Python的工作原理。函数有自己的范围。这意味着,函数内定义的变量与函数外部的变量无关。要将函数放入函数中,然后将它们传递出去,并从函数中取出函数,则返回它们的值然后将它们存储

two()因为返回元组而遇到同样的问题,但是你没有存储它 - 因此,你永远不会在main()方法中得到计算结果。< / p>

您获得错误的原因是因为您已将函数three定义为接受13个参数,所有这些都是必需的。然而,当你打电话给这个功能时,你不会传递任何东西:

three()

您需要传递参数,如下所示:

three(dice_total, count_2s, count_3s etc. etc.)

当然,在您这样做之前,请确保从其他方法中捕获返回值。