【问题标题】:How to execute this code without letting it become a never-ending loop?如何在不让它成为永无止境的循环的情况下执行此代码?
【发布时间】:2020-11-25 06:48:14
【问题描述】:

我只是尝试编写下面的代码,它只取 1 到 10 之间的整数,并打印一些输入值不满足所需条件的语句。

gamelist = list(range(0,11))

def display_game(gamelist):
    print('Here is your list of options:')
    print(gamelist)

def user_choice():
    choice = 'x'                      
    acceptable_range = range(0,11)
    within_range = False

    while choice.isdigit() == False or within_range == False:
        choice = input('Please enter a digit from the above options:')
    
        if not choice.isdigit():
            print("Oops! That's not a digit. Please try again.")
    
        if choice.isdigit():
            if int(choice) in acceptable_range:
                within_range == True
            else:
                within_range == False
                print('The entered number is out of acceptable range!')
           
    return int(choice)

display_game(gamelist)
user_choice()

在运行此代码后,即使输入了正确的值,它也会继续要求输入。我不明白到底出了什么问题以及哪里出了问题。

【问题讨论】:

  • 请做一些在线研究;所以不是这个问题的最佳地点。建议关闭。
  • 查看函数的文档。
  • 请检查如何提问,做一个简单的谷歌搜索(对于这样的问题),你会发现很多资源。此外,@AnushkaBhakare 搜索源代码和文档。就足够了,一开始您还可以在文本编辑器中使用 KIte 获取更多帮助。
  • 从问题中不清楚您是否在询问如何理解函数的源代码以查看它需要哪些参数 - 在这种情况下,请阅读任何基本的 Python 参考资料 - 或如何获得以编程方式来自函数对象的函数签名,在这种情况下,请参阅@Selcuk 链接的答案。

标签: python python-3.x


【解决方案1】:

参数通常被传递给函数访问数据,这些数据是无权访问的,但需要执行分配给它的任务。 一个很好的例子是,如果您从其他函数内部调用一个函数并希望将前一个函数的局部变量的数据用于后者,则必须将其作为 argument/arguments 传递。 这是一个非常模糊的例子,只是为了给你一些基本的提示,基本上在决定你的函数中是否需要参数之前,你会考虑很多因素!

【讨论】:

  • 虽然技术上正确,但这个答案与所提出的问题无关。
  • 我肯定误解了这个问题。
【解决方案2】:

== 用于比较,= 用于分配:

gamelist = list(range(0, 11))


def display_game(gamelist):
    print("Here is your list of options:")
    print(gamelist)


def user_choice():
    choice = "x"
    acceptable_range = range(0, 11)
    within_range = False

    while choice.isdigit() == False or within_range == False:
        choice = input("Please enter a digit from the above options:")

        if not choice.isdigit():
            print("Oops! That's not a digit. Please try again.")

        if choice.isdigit():
            if int(choice) in acceptable_range:
                # within_range == True  # this doesn't assign, but compares
                within_range = True  # this assigns
            else:
                # within_range == False  # And the same here
                within_range = False
                print("The entered number is out of acceptable range!")

    return int(choice)


display_game(gamelist)
user_choice()

这可以防止无限循环。与布尔值进行比较并不是真正必要的。保持相同的想法,这个版本更具可读性:

def user_choice():
    choice = "x"
    acceptable_range = range(0, 11)

    while not choice.isdigit() or not within_range:
        choice = input("Please enter a digit from the above options:")
        
        if not choice.isdigit():
            print("Oops! That's not a digit. Please try again.")
        else:
            within_range = int(choice) in acceptable_range
            if not within_range:
                print("The entered number is out of acceptable range!")

    return int(choice)

甚至是选择永远不是字符串的版本。但也许你还没有遇到异常:-)。 (不过,这将更改“-1”的消息。)

def user_choice():
    choice = None
    acceptable_range = range(0, 11)

    while choice not in acceptable_range:
        try:
            choice = int(input("Please enter a digit from the above options:"))
        except ValueError:
            print("Oops! That's not a digit. Please try again.")
        if choice not in acceptable_range:
            print("The entered number is out of acceptable range!")
    return choice

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 2017-11-06
    • 2023-03-22
    • 1970-01-01
    相关资源
    最近更新 更多