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()
解决方案
只需将您的game
功能放在一个loop
,
while True:
game()
推荐阅读
- python-3.x - 如何在没有时钟的情况下将整数转换为日期?
- log4j - Unirest 如何在 Java 中记录请求和响应
- awk - awk 提取失败
- javascript - 如何使用模式在 Javascript 中创建默认对象?
- html - 动画边框以相互跟随 css
- javascript - FullCalendar 对象不理解对象数组参数
- ios - 在 iPad 上横向使用时如何旋转 iPhone 应用程序
- python - sqlalchemy.exc.DatabaseError: (cx_Oracle.DatabaseError) DPI-1047: 找不到 64 位 Oracle 客户端库
- mysql - 如何从两个表中提取数据但使用一个偏移量和一个排序选项?
- java - 替代折旧的“setWiredHeadsetOn”来控制麦克风输入