简体   繁体   中英

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

this python code, when run, looks very strange when it gets output. it works, don't get me wrong, but the weird text formatting just isn't going away.

one more little thing, can someone help me with the numbers going below 0? the example shouldn't really be -5, but should just be 0 and i have no idea how i would actually do this.

('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)

this should look like:

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

I run your code and it doesn't seem to be a problem.

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

ok i've seen just the structure of ur programm without looking deeply in it. what i've seen is that there are many lines that don't seem necessary, for example your check_double function can be written like this check_double = lambda dice1, dice2: dice1 == dice2 also your check_even_odd can be written check_even_odd = lambda dice1, dice2: (dice1+dice2)%2 == 0 and yes you got it by now: roll_d6 = lambda: random.randint(1,6) Concerning your negative score, you had to add a condition in your 58 and 44 lines i think (i'm on my phone so it's pretty ankward doing python) in wich u put that u don't substracte the score if it's already zero. Anyway i'll see what i can do when i get on my laptop, but overall there are many lines that can be reduced and many conditions that can be written more efficiently. Good luck for ur programm, i'll modify your code when i get back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM