【问题标题】:python 3 logic error got me all messed uppython 3逻辑错误让我一团糟
【发布时间】:2017-02-18 04:48:59
【问题描述】:

好的,我正在为班级做一个项目,但我遇到了这个逻辑错误。在我在代码中修复它们之后,我已经注意到其余的原始错误......我觉得我知道逻辑错误在哪里。我只是不确定如何解决它。我能得到一些帮助吗?

提前致谢。

import random

def display_title():
    print("Guess the number!")
    print()

def set_limit():
    print("Enter the upper limit for the range of numbers: ")
    limit = int(input())
    return limit

def count(): ## had to add count being defined as below it was unrecognized by python. 
    count +=1

def play_game(limit):
    global count
    number = random.randint(1, limit)
    print("I'm thinking of a number from 1 to " + str(limit) + "\n")
    while True:
        guess = int(input("Your guess: "))
        if guess < number:
            print("Too low. ")
            count ## See def count
        elif guess >= number:
            print("Too high. ")
            count ## See def count
        elif guess == number: ## Pretty sure my logic error is here <---- 
            print("You guessed it in " + str(count) + " tries.\n")
            return 

def main(): ## syntax error, no : was here
    display_title()
    again = "y"
    while again.lower() == "y":
        limit = set_limit()
        play_game(limit) ## limit wasn't set inside, causing a missing positional argument 
        again = input("Play again? (y/n): ")
        print()
    print("Bye!")

if __name__ == "__main__":
    main()

【问题讨论】:

    标签: python logic


    【解决方案1】:

    我有个主意。尝试将上面的 >= 符号 3 行更改为 > 数字。因为否则它永远不会到达“ifguess == number”,因为即使猜测是正确的,它也会在每次“ifguess >= number”时停止。

    这是因为 >= 表示大于或等于。 我真的希望这会有所帮助。

    【讨论】:

    • 谢谢!!措辞像魅力!
    【解决方案2】:
    • 您不需要使用print() 来打印空行。
    • 您可以使用input 提示用户,无需单独的print 语句之前。
    • count 可以在 play_game 中定义,它应该初始化为 1 而不是默认的 0,因为即使用户在第一次尝试时猜到了,他们也猜到了 1 个。
    • elif guess &gt;= number: 需要改为elif guess &gt; number:
    • 如果answer的第一个字符转换为小写等于y,您可以再次调用main()重新启动游戏。

    我在下面的代码中修复了上述问题:

    import random
    
    def display_title():
      print("Guess the number!\n")
    
    def set_limit():
      limit = int(input("Enter the upper limit for the range of numbers: "))
      return limit
    
    def play_game(limit):
      count = 1
      number = random.randint(1, limit)
      print("I'm thinking of a number from 1 to " + str(limit) + "\n")
      while True:
        guess = int(input("Your guess: "))
        if guess < number:
          print("Too low. ")
          count += 1
        elif guess > number:
          print("Too high. ")
          count += 1
        elif guess == number: 
          print("You guessed it in " + str(count) + " tries.\n")
          break
    
    def main(): 
      display_title()
      limit = set_limit()
      play_game(limit)
      again = input("Play again? (y/n): ")
      if again[0].lower() == 'y':
        main()
      else:
        print("Bye!")
    
    if __name__ == "__main__":
      main()
    

    试试here!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-06
      • 1970-01-01
      • 1970-01-01
      • 2017-04-14
      • 1970-01-01
      • 2012-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多