【问题标题】:Issue with int/input/print in homework作业中的 int/input/print 问题
【发布时间】:2016-10-02 03:52:38
【问题描述】:

这是一个由三部分组成的问题,我遇到了问题......

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

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

  3. 计算我为其记录数据的球员......老实说,我不知道从哪里开始计算输入

--------我的输出---------- 您将被要求输入球员姓名和分数 当你没有更多的名字时,输入 End

输入玩家姓名或结束退出程序:Randy 输入 Randy 的高尔夫分数: 没有任何 ------------------我的输出END--------------------

我的代码;

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 条球员记录写入 golf.txt

---------结束赋值输出----

【问题讨论】:

  • 请修复代码中的缩进
  • 我认为我做得对吗?

标签: python input integer


【解决方案1】:

试试这个:

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()

【讨论】:

  • 其他答案也说明了同样的事情
  • 其他答案没有像你那样将 playerName 作为字符串......这就是让事情变得疯狂的原因
【解决方案2】:

你不需要在这里使用print。

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

使用这个

score = int(input("Enter {}'s golf score: ".format(playerName)))

【讨论】:

  • 当我删除打印时,我收到此错误 Traceback (last recent call last): File "G:\Software Design\Unit 4\Gossard Unit 4.py", line 27, in main () 文件“G:\Software Design\Unit 4\Gossard Unit 4.py”,第 18 行,在 main score = int(input("Enter", playerName,"'s golf score: ")) TypeError: input expected最多 1 个参数,得到 3 个
  • 不只是去掉打印语句,再读一遍代码。
【解决方案3】:
score = int(input(print("Enter", playerName, "'s golf score: ")))

应该改为

score = int(input("Enter {0}'s golf score: ".format(playerName)))

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

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

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

【讨论】:

  • 您将被要求输入球员姓名和分数当您没有更多姓名时,请输入 End Enter Player's Name 或 End 退出程序:Randy Traceback(最近一次通话最后一次):文件“G:\ Software Design\Unit 4\Gossard Unit 4.py”,第 27 行,在 main() 文件中“G:\Software Design\Unit 4\Gossard Unit 4.py”,第 18 行,在 main score = int( input("Enter", playerName,"'s golf score: ")) TypeError: input expected at most 1 arguments, got 3
  • 我已经尝试过很多次了,唯一能让玩家名字进入的方法就是使用“print”
  • 我知道我错过了很大一部分,输入只需要一个参数。看看我的编辑。
【解决方案4】:

你的线路有问题

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

你不能在输入函数里面放一个打印函数,你也不能这样做:

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

因为input 的工作方式与print 的工作方式不同。你应该在 python 中阅读formatting 并这样做:

score = int(input("Enter {0}'s golf score: ".format(playerName)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 2010-12-28
    • 2016-09-14
    • 2012-02-26
    相关资源
    最近更新 更多