“如何让用户选择使用相同的恶棍重新进行战斗。”

时间:2019-05-03 13:46:30

标签: python-3.x

我不知道如何重新运行顶部具有相同列表的代码

import random
import sys

def game():
    # Welcome sentence to this game 
    print ("Welcome!! to the game." + " In this game you can enter a list of villains to fight Avengers in a battle." + " At the end of the game we could see who won the most of the battles in the game, and then we could announce the winner.")
    # list of avengers
    Avengers = ["Iron Man", "Hulk", "Spider Man", "Loki", "Clint Barton", "Thor", "Nick Fury", "Black Widow", "Robin", "Cyclops"]
    #list of verbs
    Verbs = ["Sliced", "Stabbed", "Smashed", "Punched", "Bursted", "Blasted", "Escaped", "Doged", "Kicked", "Clouted"]
    # list of Villains from user's input
    Villains = []
    numVillains = 10

    #receive input from user to create list of villians
    for i in range(numVillains):
        Villains.append(input("Enter a Villan's name " + str(i + 1) + ": "))


    # Characters lose the game if they reach the sore of zero first
    Villains = Avengers

    while len(Avengers) > 0 or len(Villains) > 0:
        Avenger = (Avengers [random.randint(0,len(Avengers)-1)])
        Villain = (Villains [random.randint(0,len(Villains)-1)])
        Verb = (Verbs [random.randint(0,len(Verbs)-1)])                
        #Select a winner [TODO]

        if random.choice([True, False])== True:
                print(Avenger + " " + Verb+ " " + Villain )
                while Avenger: #Delete the avengers from the list after it loses in the battle
                    Avenger1 = Avengers.index(Avenger)
                    del Avengers[(int(Avenger1))]
                    break
                print ("Avengers left in the game: " + str(len(Avengers)))
                print ("Villains left in the game: " + str(len(Villains)))

        else:
                print(Villain + " " + Verb+ " " + Avenger )
                while Villain: #Delete the Villains from the list after it loses in the battle
                    Villain1 = Villains.index(Villain)
                    del Villains[(int(Villain1))]
                    break
                print ("Avengers left in the game: " + str(len(Avengers)))#prints no.of Avengers left in the game
                print ("Villains left in the game: " + str(len(Villains)))#prints no.of Villains left in the game

        print(' ')

        if len(Villains) == 0: # if villains reach the score of 0 then the avengers win the game
            print (' Avengers won the game successfully.')
            print ('Amazing!!! you won this game because Villains were defeated by Avengers.')


        if len(Avengers) == 0: # if avengers reach the score of 0 then the villains win the game
            print ('Villains won the game sucessfully.')
            print ('Ohhhh, you lost this game because Avengers were defeated by the Villains.')
game()

1 个答案:

答案 0 :(得分:0)

只需将game函数放在loop中,

while True:
    game()