Угадай вопрос с наибольшим номером - PullRequest
0 голосов
/ 23 октября 2019

Я создаю игру Python Dice под названием Beat That. До сих пор я получил все для работы, за исключением того, что игроки угадывают и сравнивают его с максимально возможным числом, которое вы можете сделать. Итак, допустим, что вы выбрасываете 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="")

1 Ответ

0 голосов
/ 23 октября 2019

Проблема будет в следующей строке:

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

Что должно решить проблему.

...