簡體   English   中英

為什么我的代碼中的 output 中有大量額外的字符?

[英]why does the output of my code have a ton of extra characters in it?

import random #importing random module

#function to roll the d6
def roll_d6():
    return random.randint(1,6)

#function to check for double roll
def check_double(dice1,dice2):
    if dice1 == dice2:
        return True
    else:
        return False

#function to check for even or odd roll
def check_even_odd(dice1,dice2):
    total = dice1 + dice2
    if total % 2 == 0:
        return True
    else:
        return False

#main game function
def game():
    player1 = input("Player 1, enter your name: ") #getting player 1 name
    player2 = input("Player 2, enter your name: ") #getting player 2 name
    print("Welcome to the game,", player1, "and", player2)
    print("Rolling the dice for 5 rounds...")
    player1_score = 0 #initializing player 1 score
    player2_score = 0 #initializing player 2 score
    for i in range(5): #5 rounds
        print("Round", i+1)
        #player 1 roll
        dice1 = roll_d6()
        dice2 = roll_d6()
        print(player1, "rolled a", dice1, "and a", dice2)
        if check_double(dice1,dice2): #if double roll
            extra_dice = roll_d6()
            print(player1, "rolled a double and got an extra roll of a", extra_dice)
            dice1 += extra_dice
        if check_even_odd(dice1,dice2): #if even roll
            player1_score += 10
            print(player1, "rolled even and gained 10 points. Total score:", player1_score)
        else: #if odd roll
            player1_score -= 5
            print(player1, "rolled odd and lost 5 points. Total score:", player1_score)
        #player 2 roll
        dice1 = roll_d6()
        dice2 = roll_d6()
        print(player2, "rolled a", dice1, "and a", dice2)
        if check_double(dice1,dice2): #if double roll
            extra_dice = roll_d6()
            print(player2, "rolled a double and got an extra roll of a", extra_dice)
            dice1 += extra_dice
        if check_even_odd(dice1,dice2): #if even roll
            player2_score += 10
            print(player2, "rolled even and gained 10 points. Total score:", player2_score)
        else: #if odd roll
            player2_score -= 5
            print(player2, "rolled odd and lost 5 points. Total score:", player2_score)
    print("Final Scores:")
    print(player1, ":", player1_score)
    print(player2, ":", player2_score)
    if player1_score > player2_score: #if player 1 wins
        print(player1, "wins!")
    elif player2_score > player1_score: #if player 2 wins
        print(player2, "wins!")
    else: #if tie
        print("It's a tie!")
        print("Rolling again to determine the winner...")
        while True:
            #player 1 roll
            dice1 = roll_d6()
            dice2 = roll_d6()
            player1_score = dice1 + dice2
            print(player1, "rolled a", dice1, "and a", dice2, "for a score of", player1_score)
            #player 2 roll
            dice1 = roll_d6()
            dice2 = roll_d6()
            player2_score = dice1 + dice2
            print(player2, "rolled a", dice1, "and a", dice2, "for a score of", player2_score)
            if player1_score > player2_score: #if player 1 wins
                print(player1, "wins!")
                break
            elif player2_score > player1_score: #if player 2 wins
                print(player2, "wins!")
                break

這段 python 代碼在運行時得到 output 時看起來很奇怪。它有效,請不要誤會我的意思,但奇怪的文本格式並沒有消失。

還有一件小事,有人能幫我把數字降到 0 以下嗎? 這個例子不應該是-5,而應該是0,我不知道我會怎么做。

('Welcome to the game,', 'a', 'and', 'b')
Rolling the dice for 5 rounds...
('Round', 1)
('a', 'rolled a', 5, 'and a', 2)
('a', 'rolled odd and lost 5 points. Total score:', -5)

這應該是這樣的:

Welcome to the game, A and B
Rolling the dice for 5 rounds...
Round 1
A rolled a 5 and a 2 
A rolled odd and lost 5 points. total score: -5

我運行你的代碼,這似乎不是問題。

def game():
    player1 = input("Player 1, enter your name: ")
    player2 = input("Player 2, enter your name: ")
    print("Welcome to the game,", player1, "and", player2)
    print("Rolling the dice for 5 rounds...")
    player1_score = 0
    player2_score = 0
    for i in range(1,6):
        print("Round", i)
        dice1 = randint(1,6)
        dice2 = randint(1,6)
        print(player1, "rolled a", dice1, "and a", dice2)
        if dice1 == dice2:
            extra_dice = randint(1,6)
            print(player1, "rolled a double and got an extra roll of a", extra_dice)
            dice1 += extra_dice
        if (dice1 + dice2) % 2 == 0:
            player1_score += 10
            print(player1, "rolled even and gained 10 points. Total score:", player1_score)
        else:
            player1_score -= 5
            print(player1, "rolled odd and lost 5 points. Total score:", player1_score)
        dice1 = randint(1,6)
        dice2 = randint(1,6)
        print(player2, "rolled a", dice1, "and a", dice2)
        if dice1 == dice2:
            extra_dice = randint(1,6)
            print(player2, "rolled a double and got an extra roll of a", extra_dice)
            dice1 += extra_dice
        if (dice1 + dice2) % 2 == 0:
            player2_score += 10
            print(player2, "rolled even and gained 10 points. Total score:", player2_score)
        else:
            player2_score -= 5
            print(player2, "rolled odd and lost 5 points. Total score:", player2_score)
    print("Final Scores:")
    print(player1, ":", player1_score)
    print(player2, ":", player2_score)
    if player1_score > player2_score:
        print(player1, "wins!")
    elif player2_score > player1_score:
        print(player2, "wins!")
    else:
        print("It's a tie!")
        print("Rolling again to determine the winner...")
        while True:
            player1_score = randint(1,6) + randint(1,6)
            print(player1, "rolled a", dice1, "and a", dice2, "for a score of", player1_score)
            player2_score = randint(1,6) + randint(1,6)
            print(player2, "rolled a", dice1, "and a", dice2, "for a score of", player2_score)
            if player1_score > player2_score:
                print(player1, "wins!")
                break
            elif player2_score > player1_score:
                print(player2, "wins!")
                break
game()

好的,我只看到了你的程序的結構,但沒有深入研究它。 我看到的是有很多行似乎沒有必要,例如你的 check_double function 可以這樣寫check_double = lambda dice1, dice2: dice1 == dice2你的 check_even_odd 也可以寫成check_even_odd = lambda dice1, dice2: (dice1+dice2)%2 == 0是的,你現在知道了: roll_d6 = lambda: random.randint(1,6)關於你的負分,你必須在你的 58 和 44 行中添加一個條件 i想一想(我在我的手機上,所以用 python 做起來很尷尬)如果你把分數已經是零的話,你就不要減去它。 無論如何,當我拿到我的筆記本電腦上時,我會看看我能做些什么,但總的來說,有很多行可以減少,很多條件可以更有效地編寫。 祝你的程序好運,我回來后會修改你的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM