【问题标题】:Need to figure out how to create a hot/cold part to guessing game in python需要弄清楚如何在python中创建一个热/冷部分来猜谜游戏
【发布时间】:2016-07-17 19:58:33
【问题描述】:

我正在上我的第一堂编程课,并在 StackOverFlow 上提出我的第一个问题。我目前正在尝试编写一个猜谜游戏程序,如果他们的猜测在用户猜测的 10 高或低范围内,我必须提醒用户。我目前正在阅读Python 入门一书的第 5 章。

教授是这样写的: 如果玩家的猜测低于生成的数字,则显示消息“太低了!”如果玩家的猜测与生成的数字相差 10 分以内但低于生成的数字,则给出消息“正在变暖但仍然低!”

我确信我已经解决了问题中的所有其他问题,但无法弄清楚如何执行“热/冷”选项。我已将我编写代码的尝试作为 cmets。

谢谢!

import random

def main ():
    guess_game ()
    try_again()

def guess_game():
    number = random.randint (1, 1000)
    guess_taken = 0
    while guess_taken < 100000000:
        print ('I am thinking of a number between 1 and 1000.')
        guess = float (input('Please enter a guess: '))
        guess_taken = guess_taken + 1
        if guess < number:
            print('Your guess is too low.')
        elif guess > number:
            print('Your guess is too high.')
        #elif (number - guess) > (number - 10):
           #print ('Guess Is Hot')
        #elif (number - guess) < (number + 10):
           #print ('Guess is cold')
        else:
            guess == number
            print('Good job! You guessed my number in', guess_taken,' guesses!')
            try_again ()
            print ()

#think about doing a range somewhere

def try_again():
    again = 'y'
    while again == 'y' or again == 'Y':
        again = input ('Would you like to play again?')
        if again == 'y' or again == 'Y':
            guess_game()
        else:
            exit ()

【问题讨论】:

  • 绝对值内置函数abs()可能对您有所帮助。试试abs(number-guess)
  • 而不是使用 4 分支 if/elif,您可以查看嵌套的 if 语句 - 例如类似if guess &lt; number: if guess &lt; (number - 10): print "too low" else: print "warmer but too low"
  • 您的guess_game 函数中不应有while 循环。您的代码告诉我您走在正确的轨道上,但您对 scope 几乎没有了解。我建议您了解scope,然后尝试重写您的解决方案。
  • @gragas 这没什么用 - while 循环有什么问题,与 [variable] 作用域有什么关系?
  • @TomDalton 实际上,我撤回了我的评论。我误读了你的程序。需要注意的一点是,您可以只使用while True:,然后使用break,而不是将用户的猜测次数限制在固定(尽管很高)。

标签: python


【解决方案1】:

我知道我迟到了,但这可能会帮助未来的人寻找这个。我对代码做了一些小的改动,结果就是这样。经过全面测试且功能齐全。

import random
import sys


def main():
    guess_the_number()
    try_again()


def guess_the_number():
    number = (random.randint(1, 1000))
    tries = 0
    print("---------------------------------------------\nYou need to guess a number between 1 and "
          "1000\n---------------------------------------------")
    while tries < 99999999999:
        guess = float(int(input("Please enter a number: ")))
        tries = tries + 1
        if guess < number:
            if (number - guess) < 10:
                print("Guess is hot")
            else:
                print("Guess is cold")
            print("Your guess is low.")
        elif guess > number:
            if (guess - number) < 10:
                print("Guess is hot")
            else:
                print("Guess is cold")
            print("Your guess is high.")
        else:
            break
    print("-------------------------------------------\nGood job! You guessed my number in", tries,
          "tries!\n-------------------------------------------")


def try_again():
    again = "y"
    while again == "y" or again == "Y" or again == "yes" or again == "Yes":
        again = input("Would you like to play again? ")
        if again == "y" or again == "Y" or again == "yes" or again == "Yes":
            return main()
        else:
            return sys.exit(0)


main()

【讨论】:

    【解决方案2】:

    您的程序将无法运行。我测试了它。一旦我制作了一个像你这样的冷热猜数游戏并回顾它,你将需要改变这一行:

    guess = float(input('Please enter a guess: '))
    

    到:

    guess = float(int(input('Please enter a guess: ')))
    

    我没有对此进行测试,但它应该可以工作。

    【讨论】:

    • 为什么您认为在float 之前转换为int 会有所帮助?
    【解决方案3】:

    我希望这有效。我现在没有调试器/控制台。

    import random
    
    def main():
        while True:
            guess_game()
            schoice = try_again()
            if schoice == False:
                exit()
    
    
    def guess_game():
        number = random.randint (1, 1000)
        guess_taken = 0
        while True:
            print ('I am thinking of a number between 1 and 1000.')
            while True:
                try:
                    guess = float (input('Please enter a guess: '))
                    break
                except:
                    continue
            guess_taken = guess_taken + 1
            if guess < number:
                if (number - guess) < 10:
                    print ('Guess is hot')
                else:
                    print ('Guess is cold')
                print('Your guess is too low.')
            elif guess > number:
                if (guess - number) < 10:
                    print ('Guess is hot')
                else:
                    print ('Guess is cold')
                print('Your guess is too high.')
            else:
                break
        print('Good job! You guessed my number in', guess_taken,' guesses!')
    
    def try_again():
        again = 'y'
        while again == 'y' or again == 'Y':
            again = input ('Would you like to play again?')
            if again == 'y' or again == 'Y':
                return True
            else:
                return False
    
    main()
    

    如果这回答了您所有的问题,那么请检查左侧上下箭头下方的勾号,中间的数字。如果没有,请在此答案下方发表评论。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-01
      • 1970-01-01
      相关资源
      最近更新 更多