我的NIM游戏代码。计算机转弯代码错误

2024-09-28 19:22:37 发布

您现在位置:Python中文网/ 问答频道 /正文

#Question 1
def playNovice(marbles):
    import random
    rand = random.randint(1,(0.5*marbles))
    print('Computer takes: ', rand)

#Question 2
def userPlay(marbles):
    userAmount = marbles
    while userAmount > (marbles * 0.5):
        userAmount = int(input("Input Number of Marbles you want to take from pile: "))
    return userAmount

#Question 4
import random
marbles = random.randint(10,100)
print("Size of Marble Pile: ", marbles)
OneOrZero = random.randint(0,1)
starter = int(input("Enter a Number between 0 and 1 to decide who starts: "))
while marbles > 1:
    if starter == OneOrZero:
        while marbles > 1:
            print("Your Turn!")
            rand = userPlay(marbles)
            marbles = (marbles - rand)
            print(marbles, 'Marbles Remaining')
            print("Computers Turn!")
            rand = playNovice(marbles)
            marbles = (marbles - rand)
            print(marbles, 'Marbles Remaining')          
    elif starter != OneOrZero:
        while marbles > 1:
            print("Computers Turn!")
            rand = playNovice(marbles)
            marbles = (marbles - rand)
            print(marbles, 'Marbles Remaining')
            print("Your Turn!")
            rand = userPlay(marbles)
            marbles = (marbles - rand)
            print(marbles, 'Marbles Remaining')

计算机取出弹珠后出错。很多时候我发现很难理解错误代码的含义,或者至少是如何修复它。在

^{pr2}$

Tags: randomturnquestionprintstarterrandintwhilerand
2条回答

如您所见,random.randint()只接受ints(整数)。在

>>> import random
>>> random.randint(1, 10)
6
>>> random.randint(1, 10)
2

但是,如果使用浮点,则会产生相同的错误:

^{pr2}$

这是你更新的代码

import math
#Question 1
def playNovice(marbles):
    import random
    rand = random.randint(1,math.floor(0.5*marbles))
    print('Computer takes: ', rand)

#Question 2
def userPlay(marbles):
    userAmount = marbles
    while userAmount > (marbles * 0.5):
        userAmount = int(input("Input Number of Marbles you want to take from pile: "))
    return userAmount

#Question 4
import random
marbles = random.randint(10,100)
print("Size of Marble Pile: ", marbles)
OneOrZero = random.randint(0,1)
starter = int(input("Enter a Number between 0 and 1 to decide who starts: "))
while marbles > 1:
    if starter == OneOrZero:
        while marbles > 1:
            print("Your Turn!")
            rand = userPlay(marbles)
            marbles = (marbles - rand)
            print(marbles, 'Marbles Remaining')
            print("Computers Turn!")
            rand = playNovice(marbles)
            marbles = (marbles - rand)
            print(marbles, 'Marbles Remaining')          
    elif starter != OneOrZero:
        while marbles > 1:
            print("Computers Turn!")
            rand = playNovice(marbles)
            marbles = (marbles - rand)
            print(marbles, 'Marbles Remaining')
            print("Your Turn!")
            rand = userPlay(marbles)
            marbles = (marbles - rand)
            print(marbles, 'Marbles Remaining')

它使用math.floor来获得底部整数,因此如果marbles是11,我们不希望它取更高(6)的randint。在

在回溯中可以看到错误:

rand = random.randint(1,(0.5*marbles))

此函数调用的第二个值将是浮点值,而不是整数。randint函数只适用于整型参数。但是,异常比通常更难理解,因为randint是引发实际异常的randrange的包装器。在

要解决这个问题,您需要使用int将参数强制为整数,或者以不同的方式进行计算(例如使用//floor division操作符)。既然这看起来像是家庭作业,我就把细节留给你。在

不过,稍后您可能会遇到另一个问题,即您的playNovice函数没有返回获取的弹珠数。你可能也要修这个!在

相关问题 更多 >