如何添加赢利或亏损变量

问题描述 投票:0回答:1

嘿,我想为 "赢了 "或 "输了 "添加一个变量,我已经有了一个玩家名字和允许猜测的变量。"任何帮助将是善良的感谢。这是我目前的代码。

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())
python variables
1个回答
1
投票

也许在你循环变量之前添加 won = False而在循环中

     if guess == number:
         won = True
         break

循环之后,如果玩家没有找到nulber won将为false,否则将为True。

用于节省

f.write( str(won) ) # convert to string
© www.soinside.com 2019 - 2024. All rights reserved.