Как добавить переменную выигрыша или проигрыша - PullRequest
0 голосов
/ 16 июня 2020

Привет, я пытаюсь добавить переменную для "выигрыша" или "проигрыша", у меня уже есть переменная для имени игрока и разрешенных предположений. Любая помощь будет любезной. Это код, который у меня есть:

import random

number = random.randint(1, 100)
player_name = input("Hello, What's your name?: ")                        
number_of_guesses = 0             

print("Okay! "+ player_name+ " I am guessing a number between 1 and 100:")

max_guesses = random.randint(1, 6)

print("You have " + str(max_guesses) + " guesses. ")

while number_of_guesses < max_guesses:

    guess = int(input())

    number_of_guesses += 1

    if guess < number:


        print("Your guess is too low")

    if guess > number:

        print("Your guess is too high")

    if guess == number:
        break

if guess == number:

    print("You guessed the number in " + str(number_of_guesses) + " tries!")

else:

    print("You did not guess the number, the number was " + str(number))


f = open("statistics.txt", "a")

f.write =(player_name) (max_guesses)

f.close()

f = open("statistics.txt", "r")

print(f.read())

1 Ответ

1 голос
/ 16 июня 2020

Может быть, добавить перед вами l oop переменную won = False И в l oop

     if guess == number:
         won = True
         break

После l oop, если игрок не найдет, выигрыш будет ложный. В другом случае будет True

Для сохранения

f.write( str(won) ) # convert to string
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...