Python 我如何给用户选择播放哪种模式的选项?

Python 我如何给用户选择播放哪种模式的选项?,python,Python,我想让用户可以选择是将坐标随机化,还是为Python上的战舰游戏输入坐标。应该注意的是,我正在使用PyScripter的一个可移植应用程序来构建这个 随机版本 from random import randint Battleship_Board =[] for x in range (0,5): Battleship_Board.append(["O"] * 5) def print_Battleship_Board(Battleship_Board): for row in Batt

我想让用户可以选择是将坐标随机化,还是为Python上的战舰游戏输入坐标。应该注意的是,我正在使用PyScripter的一个可移植应用程序来构建这个

随机版本

from random import randint

Battleship_Board =[]


for x in range (0,5):
Battleship_Board.append(["O"] * 5)

def print_Battleship_Board(Battleship_Board):
for row in Battleship_Board:
    print (" ".join(row))

print ("Let's play Battleships!")
print_Battleship_Board(Battleship_Board)

def random_row(Battleship_Board):
return randint(0, len(Battleship_Board) - 1)

def random_col(Battleship_Board):
return randint(0, len(Battleship_Board[0]) - 1)

Battleship_Row = random_row(Battleship_Board)
Battleship_Column = random_col(Battleship_Board)

for turn in range(5):
    Guess_Board_Row = int(input("Guesss the X value"))
    Guess_Board_Column = int(input("Guess the Y value"))

if Guess_Board_Row == Battleship_Row and Guess_Board_Column == Battleship_Column:
    print("You sunk my battleship!")
    break
else:

    if (Guess_Board_Row < 1 or Guess_Board_Row > 5) or (Guess_Board_Column < 1 or Guess_Board_Column > 5):
            print("Apologies, that's not on the grid")

    elif(Battleship_Board[Guess_Board_Row][Guess_Board_Column] == "X"):
            print("You already guessed that value")

    else:
        print("You missed my battleship")
        Battleship_Board[Guess_Board_Row][Guess_Board_Column] = "X"

        print("Turn" + str(turn+1) + " out of 4.")
        print_Battleship_Board(Battleship_Board)

    if turn >=4:
        print("Game Over")
Battleship_Board =[]


for x in range (0,5):
Battleship_Board.append(["O"] * 5)



def print_Battleship_Board(Battleship_Board):
for row in Battleship_Board:
    print (" ".join(row))

print ("Let's play Battleships!")
print_Battleship_Board(Battleship_Board)

Battleship_Row =  int(input("Please enter a X value"))
Battleship_Column = int(input("Please enter a Y value"))

if (Battleship_Row < 1 or Battleship_Row > 5) or (Battleship_Column < 1 or Battleship_Row > 5):
print("Apologies, that's not on the grid")

for turn in range(5):
    Guess_Board_Row = int(input("Guess the X value"))
    Guess_Board_Column = int(input("Guess the Y value"))

if Guess_Board_Row == Battleship_Row and Guess_Board_Column == Battleship_Column:
    print("You sunk my battleship!")
    print("My Ship was here: [" + str(Battleship_Row) + "][" + str(Battleship_Column) + "]")
    break

else:
    if turn == 5:
        Battleship_Board[Battleship_Row][Battleship_Column] = "X"
        print_Battleship_Board(Battleship_Board)
        print("Game Over")
        print("My Ship was here: [" + str(Battleship_Row) + "][" + str(Battleship_Column) + "]")

    else:
        if (Guess_Board_Row < 1 or Guess_Board_Row > 5) or (Guess_Board_Column < 1 or Guess_Board_Column > 5):
            print("Apologies, that's not on the grid")

        elif(Battleship_Board[Guess_Board_Row][Guess_Board_Column] == "X"):
            print("You already guessed that value")

        else:
            print("You missed my battleship!")
            Battleship_Board[Guess_Board_Row][Guess_Board_Column] = "X"

        print("Turns taken out of 5:", turn + 1)
        print_Battleship_Board(Battleship_Board)
来自随机导入randint
战列舰甲板=[]
对于范围(0,5)内的x:
战列舰甲板附加([“O”]*5)
def打印战列舰板(战列舰板):
对于战列舰甲板上的row:
打印(“.”连接(行))
打印(“让我们玩战舰吧!”)
打印战列舰板(战列舰板)
def random_行(战列舰甲板):
返回randint(0,len(战列舰甲板)-1)
def random_col(战列舰甲板):
返回randint(0,len(战列舰甲板[0])-1)
战列舰排=随机排(战列舰排)
战列舰列=随机列(战列舰板)
对于交车范围(5):
Guess_Board_Row=int(输入(“猜测X值”))
猜一猜板列=int(输入(“猜Y值”))
如果Guess_Board_Row==战舰_Row和Guess_Board_Column==战舰_Column:
打印(“你击沉了我的战舰!”)
打破
其他:
如果(猜板行<1或猜板行>5)或(猜板列<1或猜板列>5):
打印(“抱歉,这不在网格上”)
elif(战列舰甲板[Guess\u Board\u Row][Guess\u Board\u Column]=“X”):
打印(“您已经猜到了该值”)
其他:
打印(“你错过了我的战舰”)
战列舰甲板[猜测甲板排][猜测甲板列]=“X”
打印(“旋转”+str(旋转+1)+“共4次”)
打印战列舰板(战列舰板)
如果圈数>=4:
打印(“游戏结束”)
如您所见,我让用户根据列和行的随机值来玩游戏

输入的版本

from random import randint

Battleship_Board =[]


for x in range (0,5):
Battleship_Board.append(["O"] * 5)

def print_Battleship_Board(Battleship_Board):
for row in Battleship_Board:
    print (" ".join(row))

print ("Let's play Battleships!")
print_Battleship_Board(Battleship_Board)

def random_row(Battleship_Board):
return randint(0, len(Battleship_Board) - 1)

def random_col(Battleship_Board):
return randint(0, len(Battleship_Board[0]) - 1)

Battleship_Row = random_row(Battleship_Board)
Battleship_Column = random_col(Battleship_Board)

for turn in range(5):
    Guess_Board_Row = int(input("Guesss the X value"))
    Guess_Board_Column = int(input("Guess the Y value"))

if Guess_Board_Row == Battleship_Row and Guess_Board_Column == Battleship_Column:
    print("You sunk my battleship!")
    break
else:

    if (Guess_Board_Row < 1 or Guess_Board_Row > 5) or (Guess_Board_Column < 1 or Guess_Board_Column > 5):
            print("Apologies, that's not on the grid")

    elif(Battleship_Board[Guess_Board_Row][Guess_Board_Column] == "X"):
            print("You already guessed that value")

    else:
        print("You missed my battleship")
        Battleship_Board[Guess_Board_Row][Guess_Board_Column] = "X"

        print("Turn" + str(turn+1) + " out of 4.")
        print_Battleship_Board(Battleship_Board)

    if turn >=4:
        print("Game Over")
Battleship_Board =[]


for x in range (0,5):
Battleship_Board.append(["O"] * 5)



def print_Battleship_Board(Battleship_Board):
for row in Battleship_Board:
    print (" ".join(row))

print ("Let's play Battleships!")
print_Battleship_Board(Battleship_Board)

Battleship_Row =  int(input("Please enter a X value"))
Battleship_Column = int(input("Please enter a Y value"))

if (Battleship_Row < 1 or Battleship_Row > 5) or (Battleship_Column < 1 or Battleship_Row > 5):
print("Apologies, that's not on the grid")

for turn in range(5):
    Guess_Board_Row = int(input("Guess the X value"))
    Guess_Board_Column = int(input("Guess the Y value"))

if Guess_Board_Row == Battleship_Row and Guess_Board_Column == Battleship_Column:
    print("You sunk my battleship!")
    print("My Ship was here: [" + str(Battleship_Row) + "][" + str(Battleship_Column) + "]")
    break

else:
    if turn == 5:
        Battleship_Board[Battleship_Row][Battleship_Column] = "X"
        print_Battleship_Board(Battleship_Board)
        print("Game Over")
        print("My Ship was here: [" + str(Battleship_Row) + "][" + str(Battleship_Column) + "]")

    else:
        if (Guess_Board_Row < 1 or Guess_Board_Row > 5) or (Guess_Board_Column < 1 or Guess_Board_Column > 5):
            print("Apologies, that's not on the grid")

        elif(Battleship_Board[Guess_Board_Row][Guess_Board_Column] == "X"):
            print("You already guessed that value")

        else:
            print("You missed my battleship!")
            Battleship_Board[Guess_Board_Row][Guess_Board_Column] = "X"

        print("Turns taken out of 5:", turn + 1)
        print_Battleship_Board(Battleship_Board)
Battleship_Board=[]
对于范围(0,5)内的x:
战列舰甲板附加([“O”]*5)
def打印战列舰板(战列舰板):
对于战列舰甲板上的row:
打印(“.”连接(行))
打印(“让我们玩战舰吧!”)
打印战列舰板(战列舰板)
战列舰_行=int(输入(“请输入X值”))
战列舰_列=int(输入(“请输入Y值”))
如果(战列舰列<1或战列舰列>5)或(战列舰列<1或战列舰列>5):
打印(“抱歉,这不在网格上”)
对于交车范围(5):
猜测(Board_Row=int(输入(“猜测X值”))
猜一猜板列=int(输入(“猜Y值”))
如果Guess_Board_Row==战舰_Row和Guess_Board_Column==战舰_Column:
打印(“你击沉了我的战舰!”)
打印(“我的船在这里:[”+str(战列舰排)+“][”+str(战列舰列)+“]))
打破
其他:
如果匝数=5:
战列舰甲板[战列舰排][战列舰列]=“X”
打印战列舰板(战列舰板)
打印(“游戏结束”)
打印(“我的船在这里:[”+str(战列舰排)+“][”+str(战列舰列)+“]))
其他:
如果(猜板行<1或猜板行>5)或(猜板列<1或猜板列>5):
打印(“抱歉,这不在网格上”)
elif(战列舰甲板[Guess\u Board\u Row][Guess\u Board\u Column]=“X”):
打印(“您已经猜到了该值”)
其他:
打印(“你错过了我的战舰!”)
战列舰甲板[猜测甲板排][猜测甲板列]=“X”
打印(“从5圈中取出的圈数:”,圈数+1)
打印战列舰板(战列舰板)
这里是用户输入值的地方


我想在这两个版本之间建立一个选项,供用户在启动应用程序时选择。我该怎么做?

您可以使用命令行参数,允许用户在启动游戏时选择模式:

import sys

if len(sys.argv) > 1 and sys.argv[1] == '<some-value>':
    # Randomize
else:
    # Prompt user for input
导入系统 如果len(sys.argv)>1且sys.argv[1]='': #随机化 其他: #提示用户输入
更多关于命令行参数的信息。对于用户友好(阅读爱好者)的命令行选项,请查看。

一个
if
语句?我也这么认为,但考虑到我需要区分这两个版本以便python理解和运行它,我不确定如何布局所有的代码。到目前为止,您尝试了什么?尝试过菜单脚本/函数/类或类似的东西吗?我不知道从哪里开始,这就是我来这里的原因。我需要一些专业的帮助,我应该开始做。