【问题标题】:How to create an infinite in Python如何在 Python 中创建无限
【发布时间】:2019-11-17 02:25:58
【问题描述】:

我创建了 nim 游戏的一个版本,但它低于零。我设置计算机的方式总是和我一样多,但是如果计数是 4 并且我取出 3,那么轮到计算机时将是 1。然后电脑取出3,显示为-2。那么我将如何告诉程序始终保持计数高于或等于零呢?还有谁知道怎么做,这样电脑输入的金额就不会总是和我一样?

这是我的代码。

import random
Stones = random.randint(15, 30)
User = 0
YourTurn = True

print("This is a game where players take turns taking stones from a pile of stones. The player who 
takes the last stone loses.")
print("The current stone count is:", Stones)

while True:
     while YourTurn == True and Stones > 0:
    User = int(input("How many stones do you want to remove?"))
    if User == 1:
        Stones -= 1
        print("You removed 1 stone! The current stone count is:", Stones)
        YourTurn = not True            
    elif User == 2:
        Stones -= 2
        print("You removed 2 stone! The current stone count is:", Stones)    
        YourTurn = not True                       
    elif User == 3:
        Stones -= 3
        YourTurn = not True
        print("You removed 3 stone! The current stone count is:", Stones)
    else:
        print("You can only remove a maximum of 3 stones.")

    while YourTurn == False and Stones > 0:
        AI = random.randint(1, 3)
        if AI == 1:
            Stones -= 1
            print("The A.I removed 1 stone! The current stone count is:", Stones)
            YourTurn = not False
        elif AI == 2:
            Stones -= 2
            print("The A.I removed 2 stone! The current stone count is:", Stones)
            YourTurn = not False
        elif AI == 3:
            Stones -= 3
            print("The A.I removed 3 stone! The current stone count is:", Stones)
            YourTurn = not False

if Stones <= 0:
    if YourTurn == True:
        print("The A.I took the last stone it lost. You won the game!")
        break
    elif YourTurn == False:
        print("You took the last stone you lost. The A.I won the game!")
        break

【问题讨论】:

    标签: python


    【解决方案1】:

    您只需将 AI 可以轮流获得的石头数量从 random.randint(1, 3) 更改为 random.randint(1, min(3, Stones)),这样它就不能获得比剩余数量更多的石头。

    import random
    Stones = random.randint(15, 30)
    User = 0
    YourTurn = True
    
    print("This is a game where players take turns taking stones from a pile of stones. The player who takes the last stone loses.")
    print("The current stone count is:", Stones)
    
    while True:
        while YourTurn == True and Stones > 0:
            User = int(input("How many stones do you want to remove?"))
    
            if User > Stones:
                print("Your can't remove more than the number of remaining stones.")
                continue
    
            if User == 1:
                Stones -= 1
                print("You removed 1 stone! The current stone count is:", Stones)
                YourTurn = not True            
            elif User == 2:
                Stones -= 2
                print("You removed 2 stone! The current stone count is:", Stones)    
                YourTurn = not True                       
            elif User == 3:
                Stones -= 3
                YourTurn = not True
                print("You removed 3 stone! The current stone count is:", Stones)
            else:
                print("You can only remove a maximum of 3 stones.")
    
        while YourTurn == False and Stones > 0:
            AI = random.randint(1, min(3, Stones))
            if AI == 1:
                Stones -= 1
                print("The A.I removed 1 stone! The current stone count is:", Stones)
                YourTurn = not False
            elif AI == 2:
                Stones -= 2
                print("The A.I removed 2 stone! The current stone count is:", Stones)
                YourTurn = not False
            elif AI == 3:
                Stones -= 3
                print("The A.I removed 3 stone! The current stone count is:", Stones)
                YourTurn = not False
    
        if Stones <= 0:
            if YourTurn == True:
                print("The A.I took the last stone it lost. You won the game!")
                break
            elif YourTurn == False:
                print("You took the last stone you lost. The A.I won the game!")
                break
    
    

    【讨论】:

    • 谢谢,你也知道怎么做,这样电脑就不会总是像我一样带走同样数量的石头吗?
    • 由于您在AI = random.randint(1, min(3, Stones)) 中使用了random.randint 函数,因此给定代码中的计算机会随机抽取一些石头。因此,它并不总是与玩家使用相同数量的石头。您的意思是您希望计算机带走的棋子数量必须始终与玩家的不同?
    • 您是否删除了打印赢消息行下的break 行?如果是这样,那么您应该将其放回原处,因为它用于退出while True:循环
    • 谢谢你我现在明白了,是的,我只是出于某种原因休息了。
    • 但是如果玩家输入的数字太大导致最终计数变为负数怎么办?
    猜你喜欢
    • 1970-01-01
    • 2017-09-29
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 2014-05-17
    相关资源
    最近更新 更多