玩家已经猜到了这个数字

时间:2016-01-18 20:59:18

标签: python random numbers

我很难告诉玩家玩家已经猜到了这个号码。

这是我的代码:

import random

number = random.randint (1, 100)
guess = int(input("\nCan you guess the number?\n\n"))

guessing = 1

def get_guess(already_guessed):
    guess = input()
    if guess in already_guessed:
        print("You already guessed that number.")
    else:
        return guess

while guess != number:
    if guess < number:
        print("Your guess is too low.")
    if guess > number:
        print("Your guess is too high.")
    if guess == number:
        break

    guess = get_guess(input("Try again.\n\n"))

2 个答案:

答案 0 :(得分:0)

您永远不会更新可以将其添加到already_guessed功能的get_guess变量。你也有太多input。试试这个:

import random

number = random.randint (1, 100)
guess = int(input("\nCan you guess the number?\n\n"))

already_guessed = []

def get_guess(already_guessed):
    if guess in already_guessed:
        print("You already guessed that number.")
    else:
        already_guessed.append(guess)
    return already_guessed


while guess != number:
    if guess < number:
        print("Your guess is too low.")
    if guess > number:
        print("Your guess is too high.")
    if guess == number:
        break

    guess = int(input("Try again.\n\n"))
    already_guessed = get_guess(already_guessed)

答案 1 :(得分:0)

您没有维护所见的猜测列表,但您尝试使用它。

您在结尾处传递输入以代替 already_guessed 列表。

您的输入声明流程与游戏不一致。

你的终止并没有告诉玩家他/她赢了,你使用了多余的中断声明。

import random

number = random.randint (1, 100)
guess = int(input("\nCan you guess the number?\n\n"))

guessing = 1
seen = []

def get_guess(already_guessed):
    guess = int(raw_input("Your guess?"))
    if guess in already_guessed:
        print("You already guessed that number.")
    else:
        return guess

while guess != number:
    seen.append(guess)
    if guess < number:
        print("Your guess is too low.")
    elif guess > number:
        print("Your guess is too high.")
    guess = get_guess(seen)

print "You win!"