鱼C论坛

 找回密码
 立即注册
查看: 1423|回复: 2

python

[复制链接]
发表于 2023-10-13 00:09:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
该咋样才能让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? [y/n]")
                else:
                    Y = input(player1 + " Do you want to give up? [y/n]")
                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!")

        



本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-10-13 00:10:34 | 显示全部楼层
要让提示在第四次猜测后出现,可以将  hint  的初始值设为  False ,并在每次循环开始前将其重置为  True 。然后在判断是否需要提示的条件中加入  count == 4  的判断,即可在第四次猜测时出现提示。

修改后的代码如下:

  1. #introduce the game and rules briefly
  2. print("Three games, each game guess a number between 1 and 12.")
  3. print("The game will end if you guess the right number or you run out of turns, each game you can guess 3 times.")
  4. #Have each player enter their name
  5. player1 = input("Enter the name of the two players: ")
  6. player2 = input("Enter the name of the other player: ")
  7. win1 = 0
  8. win2 = 0
  9. hint = False  # 修改初始值
  10. #play 3 games total
  11. for i in range (3):
  12.     #pick a random number between 1 and 12
  13.     import random
  14.     correct_num = random.randint(1,12)
  15.     print(correct_num)
  16.    
  17.     guess = []
  18.     count = 1
  19.    
  20.     guessed_number = []
  21.     while guess != correct_num and count <= 6:
  22.         #take turns to guess the number
  23.         if count % 2 == 0:
  24.             player = player1
  25.         else:
  26.             player = player2
  27.         if count > 1:
  28.             print("You have already guessed: ",guessed_number)
  29.         guess = float(input(player+" guess a number between 1 and 12: "))
  30.         guessed_number.append(guess)
  31.         
  32.         
  33.         
  34.         
  35.         #if one player win,the game ends
  36.         if guess == correct_num:
  37.             print(player,"congratulations, you are right!")
  38.             
  39.             if player == player1:
  40.                 win1 = win1 + 1
  41.             else:
  42.                 win2 = win2 + 1
  43.         #tell them if the guess is bigger or smaller            
  44.         else:

  45.             if guess > correct_num:
  46.                 print(player,"your number is bigger.")
  47.             else:
  48.                 print(player,"your number is smaller.")
  49.             #give the hint
  50.             
  51.             if hint == True and guess != correct_num :
  52.                 if correct_num % 2 == 0:
  53.                     print("Hint: the correct number is even")
  54.                 else:
  55.                     print("Hint: the correct number is odd")  
  56.             elif count == 4:  # 添加判断
  57.                 hint = True
  58.             else:
  59.                 hint = False  
  60.             
  61.             #let one player give up part way through the game              
  62.             if count < 7:
  63.                 if player == player1:
  64.                     Y = input(player2 + " Do you want to give up? [y/n]")
  65.                 else:
  66.                     Y = input(player1 + " Do you want to give up? [y/n]")
  67.                 if Y == "n":
  68.                     count = count + 1
  69.                 elif Y == "y":
  70.                     count = count + 2   

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

  77. # add an extra congratulations to the player who wins the most games
  78. if win1 > win2:
  79.     print(player1,"congratulations! you win the most games!")
  80. elif win1 < win2:
  81.     print(player2,"congratulations! you win the most games!")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-13 00:11:10 | 显示全部楼层
如何在第四次猜测后出现hint?

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

下面是修改后的代码:
  1. # introduce the game and rules briefly
  2. print("Three games, each game guess a number between 1 and 12.")
  3. print("The game will end if you guess the right number or you run out of turns, each game you can guess 3 times.")
  4. # Have each player enter their name
  5. player1 = input("Enter the name of the two players: ")
  6. player2 = input("Enter the name of the other player: ")
  7. win1 = 0
  8. win2 = 0
  9. hint = False  # 初始化hint为False
  10. # play 3 games total
  11. for i in range(3):
  12.     # pick a random number between 1 and 12
  13.     import random
  14.     correct_num = random.randint(1, 12)
  15.     print(correct_num)
  16.     guess = []
  17.     count = 1
  18.     guessed_number = []
  19.     while guess != correct_num and count <= 6:
  20.         # take turns to guess the number
  21.         if count % 2 == 0:
  22.             player = player1
  23.         else:
  24.             player = player2
  25.         if count > 1:
  26.             print("You have already guessed: ", guessed_number)
  27.         guess = float(input(player + " guess a number between 1 and 12: "))
  28.         guessed_number.append(guess)
  29.         # if one player win, the game ends
  30.         if guess == correct_num:
  31.             print(player, "congratulations, you are right!")
  32.             if player == player1:
  33.                 win1 = win1 + 1
  34.             else:
  35.                 win2 = win2 + 1
  36.         # tell them if the guess is bigger or smaller
  37.         else:
  38.             if guess > correct_num:
  39.                 print(player, "your number is bigger.")
  40.             else:
  41.                 print(player, "your number is smaller.")
  42.         # give the hint
  43.         if hint == True and count >= 4 and guess != correct_num:
  44.             if correct_num % 2 == 0:
  45.                 print("Hint: the correct number is even")
  46.             else:
  47.                 print("Hint: the correct number is odd")
  48.         # let one player give up part way through the game
  49.         if count < 7:
  50.             if player == player1:
  51.                 Y = input(player2 + " Do you want to give up? [y/n]")
  52.             else:
  53.                 Y = input(player1 + " Do you want to give up? [y/n]")
  54.             if Y == "n":
  55.                 count = count + 1
  56.             elif Y == "y":
  57.                 count = count + 2
  58.         # if no winner, tell them they are losers
  59.         if count >= 6 and guess != correct_num and hint == False:
  60.             print(player1, "and", player2, "you are all losers.")
  61.             count = count + 6
  62.         if count == 4 and guess != correct_num:
  63.             hint = True  # 在第四次猜测后将hint设置为True
  64.     if i <= 1:
  65.         print("Now the next game begin! ")
  66. # add an extra congratulations to the player who wins the most games
  67. if win1 > win2:
  68.     print(player1, "congratulations! you win the most games!")
  69. elif win1 < win2:
  70.     print(player2, "congratulations! you win the most games!")
复制代码

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

球一个最佳答案谢谢啦!这对我非常重要!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 17:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表