有什么方法可以返回到Python中的代码行吗?

时间:2020-02-29 17:39:15

标签: python python-3.x list

您好,我有一个关于超级英雄上代码的作业,而我是编码的新手,我们甚至还没有开始GCSE级别...

Heroes = ["Batman","Wonder Woman","Superman","Spiderman"]
print(Heroes)
print("Current pilot: ",Heroes[0])
print("Co-pilot:",Heroes[1])
print ( Heroes[2],"has been temporarily replaced by Hit Girl")
Heroes[2]="Hit Girl"
print("Current heroes are now:")
print(Heroes)
print("There are two new superheroes")
print("Current heroes are now:")
Heroes.append("The Scarlet Witch")
Heroes.append("Valkryie")
print(Heroes)
Answer = None
while Answer not in ("Yes", "No"):
    Answer = input("Would you like to change a Hero? ")
    if Answer==("Yes"):
       Number=int(input("Choose a Hero from 0-5"))
       print("You will replace Hero:",Number)
       Name=str(input("Enter Hero name:"))
       print("New Hero name is:",Name)
       Heroes[Number]=Name
       print(Heroes)
    elif Answer==("No"):
        print("Ok this is the final list of Heroes")
        print(Heroes)
    else:
        print("That isn't a Yes or No answer...")


我真的很想知道我在完成该部分后如何返回“是”部分,以及仍然有“是”和“否”来结束脚本。

很抱歉,在...方面有很多问题可以寻求帮助。

2 个答案:

答案 0 :(得分:1)

我相信,仅当答案为“否”时,您才在寻找while循环的终止。最简单的方法是从包含while循环的行中删除“是”。 而答案不在(“是”,“否”)中: 变成: 而答案!=“否”:

希望有帮助。

答案 1 :(得分:0)

您要用户一直玩,直到用户对游戏说不。 为此,代码将是这样的。

Heroes = ["Batman","Wonder Woman","Superman","Spiderman"]
print(Heroes)
print("Current pilot: ",Heroes[0])
print("Co-pilot:",Heroes[1])
print ( Heroes[2],"has been temporarily replaced by Hit Girl")
Heroes[2]="Hit Girl"
print("Current heroes are now:")
print(Heroes)
print("There are two new superheroes")
print("Current heroes are now:")
Heroes.append("The Scarlet Witch")
Heroes.append("Valkryie")
print(Heroes)

while True:
    Answer = input("Would you like to change a Hero? ")
    if Answer==("Yes"):
       Number=int(input("Choose a Hero from 0-5"))
       print("You will replace Hero:",Number)
       Name=str(input("Enter Hero name:"))
       print("New Hero name is:",Name)
       Heroes[Number]=Name
       print(Heroes)
    elif Answer==("No"):
        print("Ok this is the final list of Heroes")
        print(Heroes)
        break
    else:
        print("That isn't a Yes or No answer...")