hom中的int/input/print问题

2024-10-03 06:30:57 发布

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

这是一个由三部分组成的问题,我对。。。。在

  1. 我的输出没有给我输入整数的选项,它在下一行打印“none”并跳过该选项

  2. 结束文件触发器工作不正常

  3. 计算我为之记录数据的玩家……我真的不知道从哪里开始计算输入数据

---------------我的输出---------------------- 你将被要求输入球员的名字和分数 如果没有其他名称,请输入End

输入玩家姓名或结束退出程序:Randy 输入Randy的高尔夫得分: 无 ------------------我的输出端------------------

我的代码

def main():
     #introduction to user explaining required information to be entered
     print("You will be asked to enter players names and scores")
     print("When you have no more names, enter End")

     #double blank space
     print("\n")

     #defined y/n
     anotherPlayer = "y"

     #define file path to save user input
     usersFile = open('golf.txt', 'w')

     #
     while anotherPlayer == "y":
         playerName = input("Enter Player's Name or End to exit program: ")
         score = int(input(print("Enter", playerName, "'s golf score: ")))
         usersFile.write(playerName + "," + str(score) + "\n")

         anotherPlayer = input("Is there another player? y / End: ")

     End = usersFile.close()

main()

------------结束我的代码----------------

我需要它说

-----------需要分配输出----------------

你将被要求输入球员的名字和分数 如果没有其他名称,请输入End

输入玩家姓名或结束退出程序:Randy 输入Randy的高尔夫得分:50

输入玩家姓名或结束退出程序:结束 您已将1个玩家记录写入高尔夫.txt在

-----------结束分配输出-----------------


Tags: to数据程序input选项记录玩家end
3条回答
score = int(input(print("Enter", playerName, "'s golf score: ")))

应改为

^{pr2}$

在python中打开和关闭文件的有效方法如下:

with open("filename", "w") as userFile:
    userFile.write(data)

当循环完成时,使用with open方法将为您关闭文件。在

这里不需要使用print。在

score = int(input(print("Enter", playerName, "'s golf score: ")))

用这个

^{pr2}$

试试这个:

def main():
    # introduction to user explaining required information to be entered
    print("You will be asked to enter players names and scores")
    print("When you have no more names, enter End")

    # double blank space
    print("\n")

    # defined y/n
    anotherPlayer = "y"

    # define file path to save user input
    usersFile = open('golf.txt', 'w')

    #
    while anotherPlayer == "y":
        playerName = raw_input("Enter Player's Name or End to exit program: ")
        score=input("Enter "+ str(playerName)+"'s golf score: ")
        usersFile.write(playerName + "," + str(score) + "\n")

        anotherPlayer = raw_input("Is there another player? y / End: ")

    End = usersFile.close()


main()

相关问题 更多 >