pan11 发表于 2023-10-12 04:08:13

python

这个代码,怎么样修改可以做到。
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? ")
                else:
                  Y = input(player1 + " Do you want to give up? ")
                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!")

isdkz 发表于 2023-10-12 04:08:41

1. 要实现在一局游戏中如果猜了四次再给出是奇数或者偶数的提示,需要在猜数的while循环中加入判断。当猜数次数count大于等于4时,再加上判断猜数是否等于正确数,如果不等于,则加上判断正确数的奇偶性并输出提示。

代码如下:

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")


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

代码如下:

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


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

完整代码如下:

Mike_python小 发表于 2023-10-12 04:09:21

修改方案:

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

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

代码实现如下:



# 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? ")
            else:
                Y = input(player1 + " Do you want to give up? ")
            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!")


球一个最佳答案谢谢啦!这对我非常重要!{:10_254:}{:10_254:}{:10_254:}

pan11 发表于 2023-10-12 04:10:35

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

但是如果count 大于等于4的情况下,后面的每一次猜测都会给出hint提示

陶远航 发表于 2023-10-12 04:10:44

根据你的需求,我对你的代码进行了一些修改。以下是修改后的代码:

# 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 6 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 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!\")
            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? \")
                else:
                  Y = input(player1 + \" Do you want to give up? \")
                if Y == \"n\":
                  count = count + 1
                elif Y == \"y\":
                  count = count + 2
                  guessed_number.append('give up')
      
      # give the hint
      if len(guessed_number) >= 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根据你的需求,我对你的代码进行了一些修改。以下是修改后的代码:

`python
# 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 6 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 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!\")
            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? \")
                else:
                  Y = input(player1 + \" Do you want to give up? \")
                if Y == \"n\":
                  count = count + 1
                elif Y == \"y\":
                  count = count + 2
                  guessed_number.append('give up')
      
      # give the hint
      if len(guessed_number) >= 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!\")


主要的修改点有:

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

pan11 发表于 2023-10-12 04:17:50

陶远航 发表于 2023-10-12 04:10
根据你的需求,我对你的代码进行了一些修改。以下是修改后的代码:




但是如果前四次猜测的时候,有人两次都放弃,列表里只有两个数字,但是也已经猜过四次了,也应该给出hint'提示

isdkz 发表于 2023-10-12 09:06:57

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

那就换成count%4呗

pan11 发表于 2023-10-12 19:57:38

dan但是如果中途有人放弃,那到第四次猜完的时候就不是count==4了

pan11 发表于 2023-10-12 19:58:14

isdkz 发表于 2023-10-12 09:06
那就换成count%4呗


dan但是如果中途有人放弃,那到第四次猜完的时候就不是count==4了

pan11 发表于 2023-10-12 20:04:27

isdkz 发表于 2023-10-12 09:06
那就换成count%4呗

就如果前面四次猜测,一共有两次放弃了,那count就是5,但是我仍然希望hint出现
页: [1]
查看完整版本: python