【问题标题】:Python user input not workinPython用户输入不起作用
【发布时间】:2020-07-08 00:13:17
【问题描述】:

我不确定我的代码有什么问题,但它不会返回打印语句。任何帮助表示赞赏。 编辑 1:在最后添加 firstpath(input1) vs firstpath() 什么都不做 编辑2:感谢基因的正确回复! print("欢迎来到(此处插入标题)\n\n") input1 = input("你走在街上,突然听到一声巨响。输入 Y 进行调查或输入 N 忽略:") 定义第一路径(输入1): 如果输入1 ==“Y”: print("你走向声音") elif input1 == "N": print("你继续你的路") 第一路径()

【问题讨论】:

  • 你在哪里打电话给firstpath(input1)?尝试在代码后添加。
  • 看起来缩进可能只是关闭了 - 最后的 firstpath() 在函数定义中。尝试将其退出并包含input1 作为上面建议的函数参数。

标签: python python-3.x


【解决方案1】:

您的代码可能应该是:

print("Welcome to (insert title here)\n\n")
input1 = input("You are walking down a street, and suddenly hear a loud bang. Type Y to investigate or N to ignore: ").lower()
def firstpath(local_input):
    if local_input == "y":
        print("You walk towards the sound")
    elif local_input == "n":
        print("You continue down your path")
    else:
        print('Sorry, bad answer.')
firstpath(input1)

【讨论】:

    【解决方案2】:

    在函数定义之外调用firstpath()。 您还应该在代码中添加else 条件以使其正常工作。!

    print("Welcome to (insert title here)\n\n")
    input1 = input("You are walking down a street, and suddenly hear a loud bang. Type Y to investigate or N to ignore: ")
    def firstpath(input1):
        if input1 == "Y":
            print("You walk towards the sound")
        elif input1 == "N":
            print("You continue down your path")
        else:
            print("Please enter either Y or N")
    firstpath(input1)
    

    【讨论】:

    • 还是什么都没做
    • 添加的 else 有效,谢谢!!!我不明白我做错了什么
    • @CranberryJuice input1 全局变量和 input1 局部变量造成了一些歧义。
    • 但是这还能怎么做呢?我应该将输入语句放在我的函数中吗?
    • @CranberryJuice Else 意味着其他一切。就像前两个是False
    【解决方案3】:
    print("Welcome to (insert title here)\n\n")
    input1 = input("You are walking down a street, and suddenly hear a loud bang. Type Y to investigate or N to ignore: ")
    def firstpath(input1):
        if input1 == "Y":
            print("You walk towards the sound")
        elif input1 == "N":
            print("You continue down your path")
    firstpath(input1)
    

    【讨论】:

    • Traceback(最近一次调用最后一次):文件“C:/Users/kmd20/Desktop/Python Projects/textbasedadventure/gam.py”,第 8 行,在 firstpath() TypeError: firstpath () 缺少 1 个必需的位置参数:'input1'
    【解决方案4】:

    您正在函数中调用 firstpath(input1)。试试这个:

    print("Welcome to (insert title here)\n\n")
    input1 = input("You are walking down a street, and suddenly hear a loud bang. Type Y to investigate or N to ignore: ")
    def firstpath(input1):
        if input1 == "Y":
            print("You walk towards the sound")
        elif input1 == "N":
            print("You continue down your path")
    firstpath(input1)
    

    【讨论】:

    • nope 不起作用,只是忽略所有内容并给我 Process finished with exit code 0
    • 它对我有用——也许这与你运行它的 python 版本有关?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多