猜最高数字挑战

2024-10-02 20:44:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个名为Beat That的Python骰子游戏。到目前为止,我已经做了所有的工作,除了让玩家猜测并将其与你能做的最高数字进行比较。假设你掷的是5和2,这是你能得到的最高数字52。到目前为止,当我输入正确的数字时,它总是说不正确。感谢您的帮助

在下面的屏幕截图中,除了def turn部分说“你能从你的卷中获得的最大数量是……”之外,其他一切都正常。它打印出正确的数字,但它将其标记为不完整

这是全部代码:

import random
import time
from random import randint
play_again = True

#greeting the players to the game
print("Welcome to Beat That, a small game made by Mats Muche.")

#as long as play_again is true this will repeat itself until the user decides to end the game
while play_again:
    totalnumber = []

    def rollDice(numOfDice):
        num = []
        for i in range(1,numOfDice+1):
            num = randint(1, 6)
            print("Rolling the dice... You rolled a",num)
            totalnumber.append(num)
            time.sleep(1.5)
        return num
        return totalnumber

    #this part checks the players guess to the highest number that can be made
    def turn(numOfDice):
        roll = rollDice(numOfDice)
        totalnumber.sort(reverse = True)
        print(*totalnumber , sep="")
        guess = int(input("What is the biggest number you can make?"))
        if guess == totalnumber:
            print("Correct!")
        else:
            if totalnumber != guess:
                print("Incorrect!")
                print("The highest number you could've made out of your roll is ", *totalnumber , sep="")
        time.sleep(1)
        return totalnumber

    #main code
    #rules
    print("*" * 80)
    print("Here are the rules!")
    time.sleep(1)
    print("-Players may take turns rolling a set number of dice.")
    time.sleep(1)
    print("-The aim of the game is to get the biggest number from your dice roll.")
    print("*" * 80)
    time.sleep(2)

    #amount of dice players want to use
    numOfDice = int(input("How many dice would you like to use? "))

    #start of game
    print("Player 1's turn:")
    time.sleep(1)
    p1 = turn(numOfDice)
    print("*" * 80)
    time.sleep(2)
    print("Player 2's turn:")
    time.sleep(1)
    totalnumber = []
    p2 = turn(numOfDice)
    print("*" * 80)

    #seeing who won the game (highest number made wins)
    if p1 > p2:
        print("Player 1 has won the game! Congrats!")
        time.sleep(1)
    elif p2 > p1:
        print("Player 2 has won the game! Congrats!")
        time.sleep(1)
    else:
        print("It's a tie! Try again.")
    print("*" * 80)

    #seeing if players want to play again
    again = input("Do you want to play again? Press any key except from 'n' to continue. ")
    if again[0].lower() == 'n':
        play_again = False

#if players say "n" then this message pops up and the game ends
print("End of game. Thank you for playing!")

感谢阅读:)

因为我是一个在学校的初学者。我真的不知道如何解决这样的问题

这就是问题所在

print("The highest number you could've made out of your roll is ", *totalnumber , sep="")

Tags: ofthetoyougamenumberplayif
1条回答
网友
1楼 · 发布于 2024-10-02 20:44:14

问题在于这一行:

def turn(numOfDice):
        roll = rollDice(numOfDice)
        totalnumber.sort(reverse = True)
        print(*totalnumber , sep="")
        guess = int(input("What is the biggest number you can make?"))
        if guess == totalnumber:
            print("Correct!")

这里,totalnumber是一个列表,而不是一个int。因此,您可以尝试将输入类似地设置为列表。更改:

guess = int(input("What is the biggest number you can make?"))

进入:

guess = list(map(int, input("What is the biggest number you can make?")))

这应该可以解决问题

相关问题 更多 >