需要弄清楚如何创建一个热/冷部分来猜测python中的游戏

时间:2016-03-30 01:54:20

标签: python

我正在学习我的第一个编程课程,并在StackOverFlow上提出我的第一个问题。我目前正在尝试编写一个猜谜游戏程序,我必须提醒用户他们的猜测是否在用户猜测的10或更低之内。我目前在从Python开始一书中的第5章。

教授写道: 如果玩家的猜测低于生成的数字,则显示消息“太低!”如果玩家的猜测与生成的数字相差10个点但低于生成的数字,则给出消息“变热但仍然低!”

我相信我已经在问题中找到了其他所有内容,但无法弄清楚如何进行“热/冷”选项。我已经将我的尝试写成了代码。

谢谢!

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 ()

3 个答案:

答案 0 :(得分:0)

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

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()

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

答案 1 :(得分:0)

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

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

为:

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

我没有对此进行过测试,但它应该有效。

答案 2 :(得分:0)

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

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()