Эта довольно простая игра просит пользователя угадать случайное число. Программа заканчивается, когда он угадает. Я хотел добавить простую функцию, я хотел, чтобы пользователь знал, когда он дважды использует один и тот же номер. Проблема в том, что написанное мною условие всегда истинно, оно каждый раз выводит «Вы уже пробовали это число».
Я знаю, как заставить программу работать, я просто не знаю, что написать. Было бы разумно сказать, что if guess
содержится дважды in previous_guess
.
# myNumber.py
# This game uses a home made function
def num():
import random
# Think a number
computer_number = random.randint(1, 100)
def is_same(target, number):
if target == number:
result = "Win"
elif target > number:
result = "Low"
else:
result = "High"
return result
# Start the game
print("Hello.\nI have thought of a number between 1 and 100:")
# Collect the user's guess as an integer
guess = int(input("Can you guess it?"))
# Use our function
higher_or_lower = is_same(computer_number, guess)
# run the game until the user is correct
while higher_or_lower != "Win":
if higher_or_lower == "Low":
guess = int(input("Sorry you are too low. Try again"))
else:
guess = int(input("Sorry you are too high. Try again"))
higher_or_lower = is_same(computer_number, guess)
Это та часть, которую я хотел добавить
previous_guess = [guess]
if guess in previous_guess:
print("You already tried with that number")
#previous_guess = []
#previous_guess.append(guess)
#I could also write it like this, same thing