鱼C论坛

 找回密码
 立即注册
查看: 1458|回复: 9

python

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

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

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

x
这个代码,怎么样修改可以做到。
1.在一局游戏中如果猜了四次再给出是奇数或者偶数的提示,这个猜了四次包括如果任一玩家放弃的次数!!!
就是一局游戏猜六次,如果前四次猜的过程中有一次放弃,只猜了三个数字,但是依旧会给出提示是奇数还是偶数
2.修改guess的初始定义和位置,让每个人猜之前可以看到之前猜过的
但是第一次猜之前什么也看不到





#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
#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 = correct_num + 1
    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
        
        
        guess = float(input(player+" guess a number between 1 and 12: "))
        guessed_number.append(guess)
        print("You have already guessed: ",guessed_number)
#if one player win,the game ends
        if guess == correct_num:
            print(player,"congratulations, you are right!")
            count = count + 6
            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.")
#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



#give the hint
        if 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")   

#if no winner, tell them they are losers   
        if count >= 6 and guess != correct_num:
            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-12 04:08:41 | 显示全部楼层
1. 要实现在一局游戏中如果猜了四次再给出是奇数或者偶数的提示,需要在猜数的while循环中加入判断。当猜数次数count大于等于4时,再加上判断猜数是否等于正确数,如果不等于,则加上判断正确数的奇偶性并输出提示。

代码如下:

  1. if count >= 4 and guess != correct_num:
  2.     if correct_num % 2 == 0:
  3.         print("Hint: the correct number is even")
  4.     else:
  5.         print("Hint: the correct number is odd")
复制代码


2. 要实现修改guess的初始定义和位置,让每个人猜之前可以看到之前猜过的,需要在while循环前先定义guess为空列表,然后在每次循环前输出之前猜过的数字。

代码如下:

  1. guessed_number = []
  2. while guess != correct_num and count <= 6:
  3.     if count % 2 == 0:
  4.         player = player1
  5.     else:
  6.         player = player2
  7.     print("You have already guessed: ",guessed_number)
  8.     guess = float(input(player+" guess a number between 1 and 12: "))
  9.     guessed_number.append(guess)
复制代码


注意:第一次猜之前不能输出之前猜过的数字,因此需要在while循环后加上一个判断,如果猜数次数count等于1,则不输出之前猜过的数字。

完整代码如下:
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-12 04:09:21 | 显示全部楼层
修改方案:

1. 添加一个计数器来记录每个游戏中的猜测次数,当猜测次数为4时,再给出是奇数还是偶数的提示。在每次猜测时,判断计数器是否小于等于4,如果是则进行提示;否则不提示。

2. 将guess的定义和位置移动到while循环之前,并将其初始化为一个空列表。在每次猜测时,先输出之前猜测过的数字,再让玩家猜测。

代码实现如下:


[/code]
# 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

# 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 count <= 6:
        # take turns to guess the number
        if count % 2 == 0:
            player = player1
        else:
            player = player2

        # 输出之前猜测的数字
        if len(guess) > 0:
            print(player + ",之前猜测的数字为:" + str(guess))

        guess_num = float(input(player + " guess a number between 1 and 12: "))
        guessed_number.append(guess_num)
        guess.append(guess_num)
        
        # give the hint
        if count == 4:
            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 < 6:
            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 += 1
            elif Y == "y":
                count += 2
                break

        # if one player win, the game ends
        if guess_num == correct_num:
            print(player, "congratulations, you are right!")
            if player == player1:
                win1 += 1
            else:
                win2 += 1
            break
        else:
            if guess_num > correct_num:
                print(player, "your number is bigger.")
            else:
                print(player, "your number is smaller.")

        count += 1

    # if no winner, tell them they are losers
    if count >= 6 and guess_num != correct_num:
        print(player1, "and", player2, "You are all losers.")

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

# 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!")
[/code]

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

使用道具 举报

 楼主| 发表于 2023-10-12 04:10:35 | 显示全部楼层
isdkz 发表于 2023-10-12 04:08
1. 要实现在一局游戏中如果猜了四次再给出是奇数或者偶数的提示,需要在猜数的while循环中加入判断。当猜数 ...

但是如果count 大于等于4的情况下,后面的每一次猜测都会给出hint提示
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-12 04:10:44 | 显示全部楼层
根据你的需求,我对你的代码进行了一些修改。以下是修改后的代码:

  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 6 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. # play 3 games total
  10. for i in range (3):
  11.     # pick a random number between 1 and 12
  12.     import random
  13.     correct_num = random.randint(1,12)
  14.     print(correct_num)
  15.    
  16.     guess = correct_num + 1
  17.     count = 1
  18.     guessed_number = []
  19.     while count <= 6:
  20.         # take turns to guess the number
  21.         if count % 2 == 0:
  22.             player = player1
  23.         else:
  24.             player = player2
  25.             
  26.         if count > 1:
  27.             print("You have already guessed: ",guessed_number)
  28.             
  29.         guess = float(input(player+" guess a number between 1 and 12: "))
  30.         guessed_number.append(guess)
  31.         
  32.         # if one player win,the game ends
  33.         if guess == correct_num:
  34.             print(player,"congratulations, you are right!")
  35.             count = count + 6
  36.             if player == player1:
  37.                 win1 = win1 + 1
  38.             else:
  39.                 win2 = win2 + 1
  40.         # tell them if the guess is bigger or smaller
  41.         else:
  42.             if guess > correct_num:
  43.                 print(player,"your number is bigger.")
  44.             else:
  45.                 print(player,"your number is smaller.")
  46.             # let one player give up part way through the game
  47.             if count < 7:
  48.                 if player == player1:
  49.                     Y = input(player2 + " Do you want to give up? [y/n]")
  50.                 else:
  51.                     Y = input(player1 + " Do you want to give up? [y/n]")
  52.                 if Y == "n":
  53.                     count = count + 1
  54.                 elif Y == "y":
  55.                     count = count + 2
  56.                     guessed_number.append('give up')
  57.         
  58.         # give the hint
  59.         if len(guessed_number) >= 4 and guess != correct_num :
  60.             if correct_num % 2 == 0:
  61.                 print("Hint: the correct number is even")
  62.             else:
  63.                 print("Hint: the correct number is odd")
  64.         
  65.         # if no winner, tell them they are losers
  66.         if count >= 6 and guess != correct_num:
  67.             print(player1,"and",player2,"You are all losers.")
  68.             count = count + 6
  69.     if i <= 1 :
  70.         print("Now the next game begin! ")

  71. # add an extra congratulations to the player who wins the根据你的需求,我对你的代码进行了一些修改。以下是修改后的代码:

  72. [b][/b]`python
  73. # introduce the game and rules briefly
  74. print("Three games, each game guess a number between 1 and 12.")
  75. print("The game will end if you guess the right number or you run out of turns, each game you can guess 6 times.")
  76. # Have each player enter their name
  77. player1 = input("Enter the name of the two players: ")
  78. player2 = input("Enter the name of the other player: ")
  79. win1 = 0
  80. win2 = 0
  81. # play 3 games total
  82. for i in range (3):
  83.     # pick a random number between 1 and 12
  84.     import random
  85.     correct_num = random.randint(1,12)
  86.     print(correct_num)
  87.    
  88.     guess = correct_num + 1
  89.     count = 1
  90.     guessed_number = []
  91.     while count <= 6:
  92.         # take turns to guess the number
  93.         if count % 2 == 0:
  94.             player = player1
  95.         else:
  96.             player = player2
  97.             
  98.         if count > 1:
  99.             print("You have already guessed: ",guessed_number)
  100.             
  101.         guess = float(input(player+" guess a number between 1 and 12: "))
  102.         guessed_number.append(guess)
  103.         
  104.         # if one player win,the game ends
  105.         if guess == correct_num:
  106.             print(player,"congratulations, you are right!")
  107.             count = count + 6
  108.             if player == player1:
  109.                 win1 = win1 + 1
  110.             else:
  111.                 win2 = win2 + 1
  112.         # tell them if the guess is bigger or smaller
  113.         else:
  114.             if guess > correct_num:
  115.                 print(player,"your number is bigger.")
  116.             else:
  117.                 print(player,"your number is smaller.")
  118.             # let one player give up part way through the game
  119.             if count < 7:
  120.                 if player == player1:
  121.                     Y = input(player2 + " Do you want to give up? [y/n]")
  122.                 else:
  123.                     Y = input(player1 + " Do you want to give up? [y/n]")
  124.                 if Y == "n":
  125.                     count = count + 1
  126.                 elif Y == "y":
  127.                     count = count + 2
  128.                     guessed_number.append('give up')
  129.         
  130.         # give the hint
  131.         if len(guessed_number) >= 4 and guess != correct_num :
  132.             if correct_num % 2 == 0:
  133.                 print("Hint: the correct number is even")
  134.             else:
  135.                 print("Hint: the correct number is odd")
  136.         
  137.         # if no winner, tell them they are losers
  138.         if count >= 6 and guess != correct_num:
  139.             print(player1,"and",player2,"You are all losers.")
  140.             count = count + 6
  141.     if i <= 1 :
  142.         print("Now the next game begin! ")

  143. # add an extra congratulations to the player who wins the most games
  144. if win1 > win2:
  145.     print(player1,"congratulations! you win the most games!")
  146. elif win1 < win2:
  147.     print(player2,"congratulations! you win the most games!")
复制代码


主要的修改点有:

1. 将每局游戏的猜测次数从3次改为6次。
2. 在每次猜测前,如果不是第一次猜测,都会显示已经猜过的数字。
3. 如果玩家选择放弃,会在已猜过的数字列表中添加'give up',并且放弃也算作一次猜测。
4. 在猜测次数(包括放弃)达到4次后,如果还没有猜中,会给出数字是奇数还是偶数的提示。
请注意,本回答来自GPT-4
如果问题已经解决,请设置最佳答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-10-12 04:17:50 | 显示全部楼层
陶远航 发表于 2023-10-12 04:10
根据你的需求,我对你的代码进行了一些修改。以下是修改后的代码:

但是如果前四次猜测的时候,有人两次都放弃,列表里只有两个数字,但是也已经猜过四次了,也应该给出hint'提示
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-12 09:06:57 | 显示全部楼层
pan11 发表于 2023-10-12 04:10
但是如果count 大于等于4的情况下,后面的每一次猜测都会给出hint提示

那就换成count%4呗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-10-12 19:57:38 | 显示全部楼层
dan但是如果中途有人放弃,那到第四次猜完的时候就不是count==4了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-10-12 19:58:14 | 显示全部楼层
isdkz 发表于 2023-10-12 09:06
那就换成count%4呗


dan但是如果中途有人放弃,那到第四次猜完的时候就不是count==4了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-10-12 20:04:27 | 显示全部楼层
isdkz 发表于 2023-10-12 09:06
那就换成count%4呗

就如果前面四次猜测,一共有两次放弃了,那count就是5,但是我仍然希望hint出现
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-23 00:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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