【问题标题】:Python 3.6 need help using randintPython 3.6 需要帮助使用 randint
【发布时间】:2017-02-24 15:18:50
【问题描述】:
from random import randint

Secret = randint(1, 100)

Chances = 5

BotGuess = randint(1, 100)

while(BotGuess != Secret and Chances != 0):
    BotGuess = randint(1, 100)
    print("Bot: I guess " + str(BotGuess) + "!")
    Chances -= 1

    if (BotGuess > Secret):
        print("You: Nope! Try guessing lower")

    elif (BotGuess < Secret):
        print("You: Nope! Try guessing higher")

    elif (BotGuess == Secret and Chances > 0):
        print("You: Wow! You win.")

    elif (Chances == 0):
        print("You: You're out of chances! You lose")


    else:
        print("Shouldn't be possible")

当我告诉机器人猜测更高时,我将如何实现,他的范围从 (1, 100) 到他的猜测 + 1 和 100 之间?反之亦然,当我告诉他猜得更低时。半新的python。

非常感谢有关我的代码外观和流程的任何提示!

【问题讨论】:

  • 首先,关于您的编程“风格”的一些提示。在 Python 中,如果你想遵循约定,你应该使用_ 来分隔变量名中的单词,而不是“驼峰式”。此外,if 和 while 语句中不需要括号。我建议您遵循的另一条建议是开始使用函数作为模块化代码的一种方式。
  • @nbro 我不同意你关于“camelCase”的说法如果你打开python本身的源代码,你会发现两者都被使用了。只要与您合作的任何人达成约定,任何一个都可以。事实上,我个人更喜欢 camelCase,因为下划线通常表示内部函数,例如 __call__ 等
  • @Aaron camelCase 在 Python 源代码中的某些地方使用,因为在编写这些模块时,这些约定可能仍然不存在(或不同)。在这里查看:python.org/dev/peps/pep-0008。但我同意你的观点,更重要的是要与你自己的风格保持一致,或者你的公司或团队所赋予的风格。

标签: python python-3.x loops numbers operators


【解决方案1】:

您可以使用在条件中更改的变量,而不是给randint 提供固定参数:

from random import randint

Secret = randint(1, 100)

Chances = 5

min_guess = 1
max_guess= 100

BotGuess = randint(1, 100)

while(BotGuess != Secret and Chances != 0):
    BotGuess = randint(min_guess, max_guess)
    print("Bot: I guess " + str(BotGuess) + "!")
    Chances -= 1

    if (BotGuess > Secret):
        print("You: Nope! Try guessing lower")
        max_guess = BotGuess - 1

    elif (BotGuess < Secret):
        print("You: Nope! Try guessing higher")
        min_guess = BotGuess + 1

    elif (BotGuess == Secret and Chances > 0):
        print("You: Wow! You win.")

    elif (Chances == 0):
        print("You: You're out of chances! You lose")


    else:
        print("Shouldn't be possible")

测试运行给了我这个输出:

Bot: I guess 68!
You: Nope! Try guessing higher
Bot: I guess 74!
You: Nope! Try guessing higher
Bot: I guess 90!
You: Nope! Try guessing higher
Bot: I guess 93!
You: Nope! Try guessing lower
Bot: I guess 92!
You: You're out of chances! You lose

【讨论】:

    【解决方案2】:

    您可以将猜测的范围存储在变量中,即minGuess 和maxGuess。您可以在开始时分别将它们设置为1 和100。现在,当您告诉机器人猜得更高时,只需将minGuess 变量设置为BotGuesss + 1。当然,每次您的代码中有 BotGuess = randint(1, 100) 时,您都将其替换为 BotGuess = randint(minGuess, maxGuess)。

    还有一个关于您的代码的一般提示:变量通常应以小写字母开头。您的变量以大写字母开头,通常表示一个类。这很实用,因此您可以更轻松地理解其他人的代码。 Here 是一个完整的风格指南。

    【讨论】:

      【解决方案3】:

      nbro 在评论中建议您改进写作风格。他是说你的代码应该改成这样的格式:

      import random
      
      
      def main():
          secret = random.randint(1, 100)
          chances = 5
          bot_guess = random.randint(1, 100)
          while bot_guess != secret and chances > 0:
              bot_guess = random.randint(1, 100)
              print(f'Bot: I guess {bot_guess}!')
              chances -= 1
              if bot_guess > secret:
                  print('You: Nope! Try guessing lower.')
              elif bot_guess < secret:
                  print('You: Nope! Try guessing higher.')
              elif bot_guess == secret and chances > 0:
                  print('You: Won! You win.')
              elif chances == 0:
                  print('You: You are out of chances! You lose.')
              else:
                  raise ValueError('should not be possible')
      
      
      if __name__ == '__main__':
          main()
      

      如果您想要一个更接近您所要求的程序,以下代码可能会帮助您更好地更改您自己的程序以制作更智能的机器人:

      import math
      import random
      import time
      
      
      LOW_VALUE = 1
      HIGH_VALUE = 100
      CHANCES = math.ceil(math.log(HIGH_VALUE - LOW_VALUE, 2))
      
      
      def main():
          secret = random.randint(LOW_VALUE, HIGH_VALUE)
          robot = random.Random(math.pi * time.time())
          lo, hi = LOW_VALUE, HIGH_VALUE
          for _ in range(CHANCES):
              guess = robot.randint(lo, hi)
              print(f'Bot: I guess {guess}!')
              if guess < secret:
                  print('You: Nope! Try guessing higher.')
                  lo = guess + 1
              elif guess > secret:
                  print('You: Nope! Try guessing lower.')
                  hi = guess - 1
              else:
                  print('You: Won! You win.')
                  break
          else:
              print('You: You are out of chances! You lose.')
      
      
      if __name__ == '__main__':
          main()
      

      【讨论】:

        猜你喜欢
        • 2021-07-23
        • 1970-01-01
        • 2011-08-15
        • 2011-12-13
        • 2014-11-04
        • 1970-01-01
        • 1970-01-01
        • 2018-03-30
        • 1970-01-01
        相关资源
        最近更新 更多