程序不循环

2024-07-04 05:53:39 发布

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

我很好奇为什么这个程序没有循环。该程序将运行一次,但当另一个游戏是玩它不记录赢家和退出

任何帮助都将不胜感激

下面是Python程序:

import random
random.seed()

print ("For each round please select from the following warriors: ")
print ("'C' or 'c' for Cowboy")
print ("'N' or 'n' for Ninja")
print ("'B' or 'b' for Bear")
print ("'Q' or 'q' for quit")
print()

gamesPlayed = 0

print ("Round", gamesPlayed +1, ":")
warriorStr = input("Please choose a warrior: ")
warriorStr = warriorStr.lower()

while not warriorStr.isalpha():
    print()
    print ("That's not a valid choice!")
    warriorStr = input("Please enter a weapon: ")

computer = random.choice("cnb")

count = 0
winCount = 0
lossCount = 0
tieCount = 0

if warriorStr == "n" or "N" and computer == "c" or "C":
    winCount = winCount + 1
    print ("You win")
elif warriorStr == "c" or "C" and computer == "b" or "B":
    winCount = winCount + 1
    print ("You win")
elif warriorStr == "b" or "B" and computer == "n" or "N":
    winCount = winCount + 1
    print ("You win")
elif warriorStr == "c" and computer == "n":
    lossCount = lossCount + 1
    print ("Computer wins")
elif warriorStr == "b" and computer == "c":
    lossCount = lossCount + 1
    print ("Computer wins")
elif warriorStr == "n" and computer == "b":
    lossCount = lossCount + 1
    print ("Computer wins")
elif warriorStr == "c" and computer == "c":
    tieCount = tieCount + 1
    print ("You tied")
elif  warriorStr == "n" and computer == "n":
    tieCount = tieCount + 1
    print ("You tied")
elif warriorStr == "b" and computer == "b":
    tieCount = tieCount + 1
    print ("You tied")

gamesPlayed = gamesPlayed + 1
print()
print ("Round", gamesPlayed +1, ":")
warriorStr = input("Choose a warrior: ")

if warriorStr == "q" and "Q":
    print("Game Over!")
    print ("You have played a total of", gamesPlayed, "games.")
    print ("You won", winCount, "times")
    print ("The computer won", lossCount, "times")
    print ("You tied", tieCount, "times")

Tags: orand程序youforrandomcomputerprint
1条回答
网友
1楼 · 发布于 2024-07-04 05:53:39

把整件事绕一圈。 试试这个:

warriorStr = input("Please choose a warrior: ")
warriorStr = warriorStr.lower()

acceptables = ['a','c','n','q']   

while warriorStr not 'q' and warriorStr in acceptables:

    warriorStr = input("Please enter a weapon: ")

    computer = random.choice("cnb")

    count = 0
    winCount = 0
    lossCount = 0
    tieCount = 0

    if warriorStr == "n" or "N" and computer == "c" or "C":
        winCount = winCount + 1
        print ("You win")
    elif warriorStr == "c" or "C" and computer == "b" or "B":
        winCount = winCount + 1
        print ("You win")
    elif warriorStr == "b" or "B" and computer == "n" or "N":
        winCount = winCount + 1
        print ("You win")
    elif warriorStr == "c" and computer == "n":
        lossCount = lossCount + 1
        print ("Computer wins")
    elif warriorStr == "b" and computer == "c":
        lossCount = lossCount + 1
        print ("Computer wins")
    elif warriorStr == "n" and computer == "b":
        lossCount = lossCount + 1
        print ("Computer wins")
    elif warriorStr == "c" and computer == "c":
        tieCount = tieCount + 1
        print ("You tied")
    elif  warriorStr == "n" and computer == "n":
        tieCount = tieCount + 1
        print ("You tied")
    elif warriorStr == "b" and computer == "b":
        tieCount = tieCount + 1
        print ("You tied")

    gamesPlayed = gamesPlayed + 1
    print()
    print ("Round", gamesPlayed +1, ":")
    warriorStr = input("Choose a warrior: ")

print("Game Over!")
print ("You have played a total of", gamesPlayed, "games.")
print ("You won", winCount, "times")
print ("The computer won", lossCount, "times")
print ("You tied", tieCount, "times")

相关问题 更多 >

    热门问题