pan11 发表于 2023-10-13 00:09:38

python

该咋样才能让hint在第四次猜测后出现,就是不管前面人放弃与否,都会在第四次出现






#introduce the game and rules briefly
print("Three games, each game guess a number between 1 and 12.")
print("The game will end if you guess the right number or you run out of turns, each game you can guess 3 times.")
#Have each player enter their name
player1 = input("Enter the name of the two players: ")
player2 = input("Enter the name of the other player: ")
win1 = 0
win2 = 0
hint = True
#play 3 games total
for i in range (3):
    #pick a random number between 1 and 12
    import random
    correct_num = random.randint(1,12)
    print(correct_num)
   
    guess = []
    count = 1
   
    guessed_number = []
    while guess != correct_num and count <= 6:
      #take turns to guess the number
      if count % 2 == 0:
            player = player1
      else:
            player = player2
      if count > 1:
            print("You have already guessed: ",guessed_number)
      guess = float(input(player+" guess a number between 1 and 12: "))
      guessed_number.append(guess)
      
      
      
      
      #if one player win,the game ends
      if guess == correct_num:
            print(player,"congratulations, you are right!")
            
            if player == player1:
                win1 = win1 + 1
            else:
                win2 = win2 + 1
      #tell them if the guess is bigger or smaller               
      else:

            if guess > correct_num:
                print(player,"your number is bigger.")
            else:
                print(player,"your number is smaller.")
            #give the hint
               
            if hint == True and count>=4 and guess != correct_num :
                if correct_num % 2 == 0:
                  print("Hint: the correct number is even")
                else:
                  print("Hint: the correct number is odd")
                hint = False   
            #let one player give up part way through the game            
            if count < 7:
                if player == player1:
                  Y = input(player2 + " Do you want to give up? ")
                else:
                  Y = input(player1 + " Do you want to give up? ")
                if Y == "n":
                  count = count + 1
                elif Y == "y":
                  count = count + 2   

      #if no winner, tell them they are losers   
      if count >= 6 and guess != correct_num and hint == False:
            print(player1,"and",player2,"you are all losers.")
            count = count + 6
    if i <= 1 :
      print("Now the next game begin! ")

# add an extra congratulations to the player who wins the most games
if win1 > win2:
    print(player1,"congratulations! you win the most games!")
elif win1 < win2:
    print(player2,"congratulations! you win the most games!")

      



isdkz 发表于 2023-10-13 00:10:34

要让提示在第四次猜测后出现,可以将hint的初始值设为False ,并在每次循环开始前将其重置为True 。然后在判断是否需要提示的条件中加入count == 4的判断,即可在第四次猜测时出现提示。

修改后的代码如下:

#introduce the game and rules briefly
print("Three games, each game guess a number between 1 and 12.")
print("The game will end if you guess the right number or you run out of turns, each game you can guess 3 times.")
#Have each player enter their name
player1 = input("Enter the name of the two players: ")
player2 = input("Enter the name of the other player: ")
win1 = 0
win2 = 0
hint = False# 修改初始值
#play 3 games total
for i in range (3):
    #pick a random number between 1 and 12
    import random
    correct_num = random.randint(1,12)
    print(correct_num)
   
    guess = []
    count = 1
   
    guessed_number = []
    while guess != correct_num and count <= 6:
      #take turns to guess the number
      if count % 2 == 0:
            player = player1
      else:
            player = player2
      if count > 1:
            print("You have already guessed: ",guessed_number)
      guess = float(input(player+" guess a number between 1 and 12: "))
      guessed_number.append(guess)
      
      
      
      
      #if one player win,the game ends
      if guess == correct_num:
            print(player,"congratulations, you are right!")
            
            if player == player1:
                win1 = win1 + 1
            else:
                win2 = win2 + 1
      #tell them if the guess is bigger or smaller            
      else:

            if guess > correct_num:
                print(player,"your number is bigger.")
            else:
                print(player,"your number is smaller.")
            #give the hint
            
            if hint == True and guess != correct_num :
                if correct_num % 2 == 0:
                  print("Hint: the correct number is even")
                else:
                  print("Hint: the correct number is odd")
            elif count == 4:# 添加判断
                hint = True
            else:
                hint = False
            
            #let one player give up part way through the game            
            if count < 7:
                if player == player1:
                  Y = input(player2 + " Do you want to give up? ")
                else:
                  Y = input(player1 + " Do you want to give up? ")
                if Y == "n":
                  count = count + 1
                elif Y == "y":
                  count = count + 2   

      #if no winner, tell them they are losers         
      if count >= 6 and guess != correct_num and hint == False:
            print(player1,"and",player2,"you are all losers.")
            count = count + 6
    if i <= 1 :
      print("Now the next game begin! ")

# add an extra congratulations to the player who wins the most games
if win1 > win2:
    print(player1,"congratulations! you win the most games!")
elif win1 < win2:
    print(player2,"congratulations! you win the most games!")

Mike_python小 发表于 2023-10-13 00:11:10

如何在第四次猜测后出现hint?

要在第四次猜测后出现提示,你可以在代码中添加一个计数器来跟踪猜测的次数。当计数器达到4时,即第四次猜测时,你可以将hint变量设置为True,这样就会在这一轮猜测后显示提示。

下面是修改后的代码:


# introduce the game and rules briefly
print("Three games, each game guess a number between 1 and 12.")
print("The game will end if you guess the right number or you run out of turns, each game you can guess 3 times.")
# Have each player enter their name
player1 = input("Enter the name of the two players: ")
player2 = input("Enter the name of the other player: ")
win1 = 0
win2 = 0
hint = False# 初始化hint为False
# play 3 games total
for i in range(3):
    # pick a random number between 1 and 12
    import random

    correct_num = random.randint(1, 12)
    print(correct_num)

    guess = []
    count = 1

    guessed_number = []
    while guess != correct_num and count <= 6:
      # take turns to guess the number
      if count % 2 == 0:
            player = player1
      else:
            player = player2
      if count > 1:
            print("You have already guessed: ", guessed_number)
      guess = float(input(player + " guess a number between 1 and 12: "))
      guessed_number.append(guess)

      # if one player win, the game ends
      if guess == correct_num:
            print(player, "congratulations, you are right!")

            if player == player1:
                win1 = win1 + 1
            else:
                win2 = win2 + 1
      # tell them if the guess is bigger or smaller
      else:
            if guess > correct_num:
                print(player, "your number is bigger.")
            else:
                print(player, "your number is smaller.")

      # give the hint
      if hint == True and count >= 4 and guess != correct_num:
            if correct_num % 2 == 0:
                print("Hint: the correct number is even")
            else:
                print("Hint: the correct number is odd")

      # let one player give up part way through the game
      if count < 7:
            if player == player1:
                Y = input(player2 + " Do you want to give up? ")
            else:
                Y = input(player1 + " Do you want to give up? ")
            if Y == "n":
                count = count + 1
            elif Y == "y":
                count = count + 2

      # if no winner, tell them they are losers
      if count >= 6 and guess != correct_num and hint == False:
            print(player1, "and", player2, "you are all losers.")
            count = count + 6

      if count == 4 and guess != correct_num:
            hint = True# 在第四次猜测后将hint设置为True

    if i <= 1:
      print("Now the next game begin! ")

# add an extra congratulations to the player who wins the most games
if win1 > win2:
    print(player1, "congratulations! you win the most games!")
elif win1 < win2:
    print(player2, "congratulations! you win the most games!")



现在,当进入第四次猜测时,无论前面的玩家是否放弃,提示都会显示在屏幕上。

球一个最佳答案谢谢啦!这对我非常重要!{:10_254:}{:10_254:}{:10_254:}
页: [1]
查看完整版本: python