猜猜最高数字问题
Guess the highest number issue
我正在创建一个名为 Beat That 的 Python 骰子游戏。到目前为止,除了让玩家猜测并将其与您可以制作的最高数字进行比较外,我已经完成了所有工作。因此,假设您掷出 5 和 2 的最高数字,您可以使它成为 52。到目前为止,当我输入正确的数字时,它总是说不正确。感谢任何帮助。
在下面的屏幕截图中,除了显示 "The highest number you could've made out of your roll is ..." 的 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="")
问题出在这一行:
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
是一个列表,不是一个整数。因此,您也可以尝试使输入类似地成为一个列表。变化:
guess = int(input("What is the biggest number you can make?"))
进入:
guess = list(map(int, input("What is the biggest number you can make?")))
应该可以解决问题。
我正在创建一个名为 Beat That 的 Python 骰子游戏。到目前为止,除了让玩家猜测并将其与您可以制作的最高数字进行比较外,我已经完成了所有工作。因此,假设您掷出 5 和 2 的最高数字,您可以使它成为 52。到目前为止,当我输入正确的数字时,它总是说不正确。感谢任何帮助。
在下面的屏幕截图中,除了显示 "The highest number you could've made out of your roll is ..." 的 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="")
问题出在这一行:
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
是一个列表,不是一个整数。因此,您也可以尝试使输入类似地成为一个列表。变化:
guess = int(input("What is the biggest number you can make?"))
进入:
guess = list(map(int, input("What is the biggest number you can make?")))
应该可以解决问题。