我需要帮助更改范围并向用户显示范围是什么,以便他们知道他们是否更近。我已经给出了我已经给出的描述。关于我需要做什么。我已经给出了到目前为止我想出的代码。如果您需要我提供任何其他信息,请告诉我。
<块引用>第 6 步 – 指导用户选择值范围
添加功能,以便在显示猜测提示时显示当前范围 根据用户的猜测来猜测过高和过高的值 低的。它将首先说明您在 1 和 100 之间的猜测是多少,包括在内?,但是作为 用户根据数值越高猜测范围会越来越小 或低于用户的猜测,例如,您的猜测在 15 到 32 之间, 包括的?下面的示例输出应该有助于澄清。
示例
$players = [
['playername' =>'Player A','team' => 'Team A','pts' => 10],
['playername' =>'Player B','team' => 'Team B','pts' => 20],
['playername' =>'Player C','team' => 'Team C','pts' => 5]
];
function pointsASC($player1, $player2){
if ($player1['pts'] == $player2['pts']) {
return 0;
}elseif($player1['pts'] < $player2['pts']){
return 1;
}elseif($player1['pts'] > $player2['pts']){
return -1;
}
}
usort($players,'pointsASC');
echo "<pre>";
print_r($players);
/*
[
['playername' =>'Player B','team' => 'Team B','pts' => 20],
['playername' =>'Player A','team' => 'Team A','pts' => 10],
['playername' =>'Player C','team' => 'Team C','pts' => 5]
];
*/
----------------
What is your guess between 1 and 44 inclusive? 2
Your guess was too low. Guess again.
答案 0 :(得分:0)
您需要的是一个存放用户最低和最高猜测的地方。然后您将使用这些进行范围检查,而不是硬编码的 1
和 11
。对于每个猜测,如果它是有效的,那么您会将其与最低和最高值进行比较,如果它低于最低值,则将最低值设置为猜测值,如果它高于最高值,它将设置猜测的最高值。最后,您需要更新 input()
字符串以显示最低和最高猜测,而不是硬编码的 '1'
和 '10'
。
答案 1 :(得分:0)
您需要大量简化您的代码。就像你问一个新值的地方大约有 6 个不同的地方,应该只有一个,也不要递归调用方法(在 again()
中调用 again()
)和再次>播放>再次之间的这种调用.
使用外部 while
循环来运行游戏,在内部使用 while
内部循环来运行游戏,最重要的是跟踪 lower_bound
和 upper_bound
>
import random
import sys
def main():
print("Assignment 6 BY enter name.")
welcome()
redo = "Y"
while redo.upper() == "Y":
print("Let’s play")
play()
redo = input("Do you want to play again (Y or N)?: ")
def welcome():
print("Welcome to the guessing game. I have selected a number between 1 and 100 inclusive. ")
print("Your goal is to guess it in as few guesses as possible. Let’s get started.\n")
def play():
lower_bound, upper_bound = 0, 100
number = int(random.randrange(lower_bound, upper_bound))
print(number)
guess = -1
number_of_guess = 0
while guess != number:
guess = int(input(f"What is your guess between {lower_bound} and {upper_bound - 1} inclusive ?: "))
if guess == -999:
print("Thanks for Playing")
sys.exit(0)
elif guess not in list(range(lower_bound, upper_bound)):
print("You're outside the range")
continue
number_of_guess += 1
if guess < number:
print("Your guess was too low")
lower_bound = guess
elif guess > number:
print("Your guess was too high")
upper_bound = guess
print("Congratulations you won in", number_of_guess, "tries!")